forked from samarthagarwal/FlutterScreens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip_loader.dart
190 lines (170 loc) · 5.86 KB
/
flip_loader.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import 'package:flutter/material.dart';
import 'dart:math';
class FlipLoader extends StatefulWidget {
final Color loaderBackground;
final Color iconColor;
final IconData icon;
final String animationType;
final String shape;
final bool rotateIcon;
FlipLoader({this.loaderBackground = Colors.redAccent, this.iconColor = Colors.white, this.icon = Icons.sync, this.animationType = "full_flip", this.shape = "square", this.rotateIcon = true});
@override
_FlipLoaderState createState() => _FlipLoaderState(this.loaderBackground, this.iconColor, this.icon, this.animationType, this.shape, this.rotateIcon);
}
class _FlipLoaderState extends State<FlipLoader>
with SingleTickerProviderStateMixin {
AnimationController controller;
Animation<double> rotationHorizontal;
Animation<double> rotationVertical;
Color loaderColor;
Color iconColor;
IconData icon;
Widget loaderIconChild;
String animationType;
String shape;
bool rotateIcon;
_FlipLoaderState(this.loaderColor, this.iconColor, this.icon, this.animationType, this.shape, this.rotateIcon);
@override
void initState() {
super.initState();
controller = createAnimationController(animationType);
controller.addStatusListener((status){
// Play animation backwards and forwards for full flip
if (animationType == "half_flip") {
if (status == AnimationStatus.completed) {
setState(() {
controller.repeat();
});
}
}
// play animation on repeat for half flip
else if (animationType == "full_flip") {
if (status == AnimationStatus.dismissed) {
setState(() {
controller.forward();
});
}
if (status == AnimationStatus.completed) {
setState(() {
controller.repeat();
});
}
}
// custom animation state
else {
print("TODO not sure yet");
}
});
controller.forward();
}
AnimationController createAnimationController([String type = 'full_flip']) {
AnimationController animCtrl;
switch(type) {
case "half_flip":
animCtrl = AnimationController(duration: const Duration(milliseconds: 4000), vsync: this);
// Horizontal animation
this.rotationHorizontal = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: animCtrl,
curve: Interval(0.0, 0.50, curve: Curves.linear)));
// Vertical animation
this.rotationVertical = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: animCtrl,
curve: Interval(0.50, 1.0, curve: Curves.linear)));
break;
case "full_flip":
default:
animCtrl = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this);
this.rotationHorizontal = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: animCtrl,
curve: Interval(0.0, 0.50, curve: Curves.linear)));
this.rotationVertical = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: animCtrl,
curve: Interval(0.50, 1.0, curve: Curves.linear)));
break;
}
return animCtrl;
}
@override
Widget build(BuildContext context) {
if (animationType == "half_flip") {
return buildHalfFlipper(context);
} else {
return buildFullFlipper(context);
}
}
Widget buildHalfFlipper(BuildContext context) {
return new AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child) {
return Container(
child: new Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.006)
..rotateX(sin(2*pi * rotationVertical.value))
..rotateY(sin(2*pi * rotationHorizontal.value)),
alignment: Alignment.center,
child: Container(
decoration: BoxDecoration(
shape: shape == "circle" ? BoxShape.circle : BoxShape.rectangle,
borderRadius: shape == "circle" ? null : new BorderRadius.all(const Radius.circular(8.0)),
color: loaderColor,
),
width: 40.0,
height: 40.0,
child: rotateIcon == true ? new RotationTransition(
turns: rotationHorizontal.value == 1.0 ? rotationVertical : rotationHorizontal,
child: new Center(
child: Icon(
icon,
color: iconColor,
size: 20.0,
),
),
) : Center(
child: Icon(
icon,
color: iconColor,
size: 20.0,
),
)
),
),
);
},
);
}
Widget buildFullFlipper(BuildContext context) {
return new AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child) {
return Container(
child: new Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.006)
..rotateX((2* pi * rotationVertical.value))
..rotateY((2* pi * rotationHorizontal.value)),
alignment: Alignment.center,
child: Container(
decoration: BoxDecoration(
shape: shape == "circle" ? BoxShape.circle : BoxShape.rectangle,
borderRadius: shape == "circle" ? null : new BorderRadius.all(const Radius.circular(8.0)),
color: loaderColor,
),
width: 40.0,
height: 40.0,
child: new Center(
child: Icon(
icon, color: iconColor, size: 20.0,
),
),
),
),
);
},
);
}
}