Skip to content

Commit ecfb398

Browse files
author
Jacob Leveroni
committed
consolidated both loaders into a single file
1 parent 4eaca1e commit ecfb398

File tree

3 files changed

+111
-115
lines changed

3 files changed

+111
-115
lines changed

lib/loaders/flip_loader.dart

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import 'package:flutter/material.dart';
2-
import 'dart:async';
32
import 'dart:math';
43

54
class FlipLoader extends StatefulWidget {
65
Color loaderBackground;
76
Color iconColor;
87
IconData icon;
8+
String animationType;
99

10-
FlipLoader({this.loaderBackground = Colors.redAccent, this.iconColor = Colors.white, this.icon = Icons.sync});
10+
FlipLoader({this.loaderBackground = Colors.redAccent, this.iconColor = Colors.white, this.icon = Icons.sync, this.animationType = "full_flip"});
1111

1212

1313
@override
14-
_FlipLoaderState createState() => _FlipLoaderState(this.loaderBackground, this.iconColor, this.icon);
14+
_FlipLoaderState createState() => _FlipLoaderState(this.loaderBackground, this.iconColor, this.icon, this.animationType);
1515
}
1616

1717
class _FlipLoaderState extends State<FlipLoader>
@@ -23,40 +23,97 @@ class _FlipLoaderState extends State<FlipLoader>
2323
Color loaderColor;
2424
Color iconColor;
2525
IconData icon;
26-
double verticalRotation = 0.0;
27-
double horizontalRotation = 0.0;
26+
Widget loaderIconChild;
27+
String animationType;
2828

29-
_FlipLoaderState(this.loaderColor, this.iconColor, this.icon);
29+
_FlipLoaderState(this.loaderColor, this.iconColor, this.icon, this.animationType);
3030

3131
@override
3232
void initState() {
3333
super.initState();
3434

35-
controller = AnimationController(
36-
duration: const Duration(milliseconds: 4000), vsync: this);
37-
rotationHorizontal = Tween<double>(begin: -1.0, end: 1.0).animate(
38-
CurvedAnimation(
39-
parent: controller,
40-
curve: Interval(0.0, 0.50, curve: Curves.linear)));
41-
rotationVertical = Tween<double>(begin: -1.0, end: 1.0).animate(
42-
CurvedAnimation(
43-
parent: controller,
44-
curve: Interval(0.50, 1.0, curve: Curves.linear)));
45-
35+
controller = createAnimationController(animationType);
4636

4737
controller.addStatusListener((status){
48-
if (status == AnimationStatus.completed) {
49-
setState(() {
50-
controller.repeat();
51-
});
38+
// Play animation backwards and forwards for full flip
39+
if (animationType == "half_flip") {
40+
print('half line 40');
41+
if (status == AnimationStatus.completed) {
42+
setState(() {
43+
controller.repeat();
44+
});
45+
}
46+
}
47+
// play animation on repeat for half flip
48+
else if (animationType == "full_flip") {
49+
print('full line 57');
50+
if (status == AnimationStatus.completed) {
51+
setState(() {
52+
controller.reverse();
53+
});
54+
}
55+
if (status == AnimationStatus.dismissed) {
56+
setState(() {
57+
controller.forward();
58+
});
59+
}
60+
}
61+
// custom animation state
62+
else {
63+
print("TODO not sure yet");
5264
}
5365
});
5466

5567
controller.forward();
5668
}
5769

70+
AnimationController createAnimationController([String type = 'full_flip']) {
71+
AnimationController animCtrl;
72+
73+
switch(type) {
74+
case "half_flip":
75+
animCtrl = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this);
76+
77+
// Horizontal animation
78+
this.rotationHorizontal = Tween<double>(begin: 0.0, end: 1.0).animate(
79+
CurvedAnimation(
80+
parent: animCtrl,
81+
curve: Interval(0.0, 0.50, curve: Curves.fastOutSlowIn)));
82+
83+
// Vertical animation
84+
this.rotationVertical = Tween<double>(begin: 0.0, end: 1.0).animate(
85+
CurvedAnimation(
86+
parent: animCtrl,
87+
curve: Interval(0.50, 1.0, curve: Curves.fastOutSlowIn)));
88+
break;
89+
case "full_flip":
90+
default:
91+
animCtrl = AnimationController(duration: const Duration(milliseconds: 4000), vsync: this);
92+
93+
this.rotationHorizontal = Tween<double>(begin: -1.0, end: 1.0).animate(
94+
CurvedAnimation(
95+
parent: animCtrl,
96+
curve: Interval(0.0, 0.50, curve: Curves.linear)));
97+
this.rotationVertical = Tween<double>(begin: -1.0, end: 1.0).animate(
98+
CurvedAnimation(
99+
parent: animCtrl,
100+
curve: Interval(0.50, 1.0, curve: Curves.linear)));
101+
break;
102+
}
103+
104+
return animCtrl;
105+
}
106+
58107
@override
59108
Widget build(BuildContext context) {
109+
if (animationType == "half_flip") {
110+
return buildFullFlipper(context);
111+
} else {
112+
return buildHalfFlipper(context);
113+
}
114+
}
115+
116+
Widget buildHalfFlipper(BuildContext context) {
60117
return new AnimatedBuilder(
61118
animation: controller,
62119
builder: (BuildContext context, Widget child) {
@@ -88,4 +145,34 @@ class _FlipLoaderState extends State<FlipLoader>
88145
},
89146
);
90147
}
148+
149+
Widget buildFullFlipper(BuildContext context) {
150+
return new AnimatedBuilder(
151+
animation: controller,
152+
builder: (BuildContext context, Widget child) {
153+
return Container(
154+
child: new Transform(
155+
transform: Matrix4.identity()
156+
..setEntry(3, 2, 0.006)
157+
..rotateX((pi * rotationVertical.value))
158+
..rotateY((pi * rotationHorizontal.value)),
159+
alignment: Alignment.center,
160+
child: Container(
161+
decoration: BoxDecoration(
162+
borderRadius: new BorderRadius.all(const Radius.circular(8.0)),
163+
color: loaderColor,
164+
),
165+
width: 40.0,
166+
height: 40.0,
167+
child: new Center(
168+
child: Icon(
169+
icon, color: iconColor
170+
),
171+
),
172+
),
173+
),
174+
);
175+
},
176+
);
177+
}
91178
}

lib/loaders/flip_loader_2.dart

Lines changed: 0 additions & 90 deletions
This file was deleted.

lib/main.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'loaders/color_loader.dart';
33
import 'loaders/flip_loader.dart';
4-
import 'loaders/flip_loader_2.dart';
54

65
void main() => runApp(new MyApp());
76

@@ -49,9 +48,9 @@ class _MyHomePageState extends State<MyHomePage> {
4948
duration: Duration(milliseconds: 1200)
5049
),
5150
Divider(height: 150.0, color: Colors.white,),
52-
FlipLoader2(loaderBackground: Colors.black, iconColor: Colors.red, icon: Icons.strikethrough_s,),
53-
Divider(height: 150.0, color: Colors.white,),
54-
FlipLoader(loaderBackground: Colors.orangeAccent, iconColor: Colors.blueAccent, icon: Icons.subway),
51+
FlipLoader(loaderBackground: Colors.black, iconColor: Colors.red, icon: Icons.strikethrough_s, animationType: "full_flip"),
52+
Divider(height: 150.0, color: Colors.white),
53+
FlipLoader(loaderBackground: Colors.blueAccent, iconColor: Colors.orangeAccent, icon: Icons.subway, animationType: "half_flip"),
5554
],
5655
),
5756
),

0 commit comments

Comments
 (0)