Skip to content

Commit 0c32c00

Browse files
Fixed Flips and Animations
1 parent 0598c7c commit 0c32c00

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

lib/loaders/flip_loader.dart

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import 'package:flutter/material.dart';
22
import 'dart:math';
33

44
class FlipLoader extends StatefulWidget {
5-
Color loaderBackground;
6-
Color iconColor;
7-
IconData icon;
8-
String animationType;
5+
final Color loaderBackground;
6+
final Color iconColor;
7+
final IconData icon;
8+
final String animationType;
9+
final String shape;
10+
final bool rotateIcon;
911

10-
FlipLoader({this.loaderBackground = Colors.redAccent, this.iconColor = Colors.white, this.icon = Icons.sync, this.animationType = "full_flip"});
12+
FlipLoader({this.loaderBackground = Colors.redAccent, this.iconColor = Colors.white, this.icon = Icons.sync, this.animationType = "full_flip", this.shape = "square", this.rotateIcon = true});
1113

1214

1315
@override
14-
_FlipLoaderState createState() => _FlipLoaderState(this.loaderBackground, this.iconColor, this.icon, this.animationType);
16+
_FlipLoaderState createState() => _FlipLoaderState(this.loaderBackground, this.iconColor, this.icon, this.animationType, this.shape, this.rotateIcon);
1517
}
1618

1719
class _FlipLoaderState extends State<FlipLoader>
@@ -25,8 +27,10 @@ class _FlipLoaderState extends State<FlipLoader>
2527
IconData icon;
2628
Widget loaderIconChild;
2729
String animationType;
30+
String shape;
31+
bool rotateIcon;
2832

29-
_FlipLoaderState(this.loaderColor, this.iconColor, this.icon, this.animationType);
33+
_FlipLoaderState(this.loaderColor, this.iconColor, this.icon, this.animationType, this.shape, this.rotateIcon);
3034

3135
@override
3236
void initState() {
@@ -37,7 +41,6 @@ class _FlipLoaderState extends State<FlipLoader>
3741
controller.addStatusListener((status){
3842
// Play animation backwards and forwards for full flip
3943
if (animationType == "half_flip") {
40-
print('half line 40');
4144
if (status == AnimationStatus.completed) {
4245
setState(() {
4346
controller.repeat();
@@ -46,15 +49,14 @@ class _FlipLoaderState extends State<FlipLoader>
4649
}
4750
// play animation on repeat for half flip
4851
else if (animationType == "full_flip") {
49-
print('full line 57');
50-
if (status == AnimationStatus.completed) {
52+
if (status == AnimationStatus.dismissed) {
5153
setState(() {
52-
controller.reverse();
54+
controller.forward();
5355
});
5456
}
55-
if (status == AnimationStatus.dismissed) {
57+
if (status == AnimationStatus.completed) {
5658
setState(() {
57-
controller.forward();
59+
controller.repeat();
5860
});
5961
}
6062
}
@@ -72,29 +74,29 @@ class _FlipLoaderState extends State<FlipLoader>
7274

7375
switch(type) {
7476
case "half_flip":
75-
animCtrl = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this);
77+
animCtrl = AnimationController(duration: const Duration(milliseconds: 4000), vsync: this);
7678

7779
// Horizontal animation
7880
this.rotationHorizontal = Tween<double>(begin: 0.0, end: 1.0).animate(
7981
CurvedAnimation(
8082
parent: animCtrl,
81-
curve: Interval(0.0, 0.50, curve: Curves.fastOutSlowIn)));
83+
curve: Interval(0.0, 0.50, curve: Curves.linear)));
8284

8385
// Vertical animation
8486
this.rotationVertical = Tween<double>(begin: 0.0, end: 1.0).animate(
8587
CurvedAnimation(
8688
parent: animCtrl,
87-
curve: Interval(0.50, 1.0, curve: Curves.fastOutSlowIn)));
89+
curve: Interval(0.50, 1.0, curve: Curves.linear)));
8890
break;
8991
case "full_flip":
9092
default:
91-
animCtrl = AnimationController(duration: const Duration(milliseconds: 4000), vsync: this);
93+
animCtrl = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this);
9294

93-
this.rotationHorizontal = Tween<double>(begin: -1.0, end: 1.0).animate(
95+
this.rotationHorizontal = Tween<double>(begin: 0.0, end: 1.0).animate(
9496
CurvedAnimation(
9597
parent: animCtrl,
9698
curve: Interval(0.0, 0.50, curve: Curves.linear)));
97-
this.rotationVertical = Tween<double>(begin: -1.0, end: 1.0).animate(
99+
this.rotationVertical = Tween<double>(begin: 0.0, end: 1.0).animate(
98100
CurvedAnimation(
99101
parent: animCtrl,
100102
curve: Interval(0.50, 1.0, curve: Curves.linear)));
@@ -107,9 +109,9 @@ class _FlipLoaderState extends State<FlipLoader>
107109
@override
108110
Widget build(BuildContext context) {
109111
if (animationType == "half_flip") {
110-
return buildFullFlipper(context);
111-
} else {
112112
return buildHalfFlipper(context);
113+
} else {
114+
return buildFullFlipper(context);
113115
}
114116
}
115117

@@ -121,18 +123,27 @@ class _FlipLoaderState extends State<FlipLoader>
121123
child: new Transform(
122124
transform: Matrix4.identity()
123125
..setEntry(3, 2, 0.006)
124-
..rotateX(sin(pi * rotationVertical.value))
125-
..rotateY(sin(pi * rotationHorizontal.value)),
126+
..rotateX(sin(2*pi * rotationVertical.value))
127+
..rotateY(sin(2*pi * rotationHorizontal.value)),
126128
alignment: Alignment.center,
127129
child: Container(
128130
decoration: BoxDecoration(
129-
borderRadius: new BorderRadius.all(const Radius.circular(8.0)),
131+
shape: shape == "circle" ? BoxShape.circle : BoxShape.rectangle,
132+
borderRadius: shape == "circle" ? null : new BorderRadius.all(const Radius.circular(8.0)),
130133
color: loaderColor,
131134
),
132135
width: 40.0,
133136
height: 40.0,
134-
child: new RotationTransition(
137+
child: rotateIcon == true ? new RotationTransition(
135138
turns: rotationHorizontal.value == 1.0 ? rotationVertical : rotationHorizontal,
139+
child: new Center(
140+
child: Icon(
141+
icon,
142+
color: iconColor,
143+
size: 20.0,
144+
),
145+
),
146+
) : Center(
136147
child: Icon(
137148
icon,
138149
color: iconColor,
@@ -154,19 +165,20 @@ class _FlipLoaderState extends State<FlipLoader>
154165
child: new Transform(
155166
transform: Matrix4.identity()
156167
..setEntry(3, 2, 0.006)
157-
..rotateX((pi * rotationVertical.value))
158-
..rotateY((pi * rotationHorizontal.value)),
168+
..rotateX((2* pi * rotationVertical.value))
169+
..rotateY((2* pi * rotationHorizontal.value)),
159170
alignment: Alignment.center,
160171
child: Container(
161172
decoration: BoxDecoration(
162-
borderRadius: new BorderRadius.all(const Radius.circular(8.0)),
173+
shape: shape == "circle" ? BoxShape.circle : BoxShape.rectangle,
174+
borderRadius: shape == "circle" ? null : new BorderRadius.all(const Radius.circular(8.0)),
163175
color: loaderColor,
164176
),
165177
width: 40.0,
166178
height: 40.0,
167179
child: new Center(
168180
child: Icon(
169-
icon, color: iconColor
181+
icon, color: iconColor, size: 20.0,
170182
),
171183
),
172184
),

0 commit comments

Comments
 (0)