Skip to content

Commit 1b5fcf5

Browse files
author
Vivek Chib
committed
added animatedOpacity widget to opacity box and fixed when animation is reversing and play/pause force it go forward
1 parent 54aa30f commit 1b5fcf5

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

lib/views/home_page.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
100100
void playPauseAnimation() {
101101
if (controller.isAnimating) {
102102
controller.stop();
103-
playPauseController.animateBack(0.0);
103+
playPauseController.reverse();
104104
} else {
105-
controller.repeat(reverse: true);
105+
if (controller.isForwardOrCompleted) {
106+
controller.repeat(reverse: true);
107+
} else {
108+
controller.reverse().then((value) => controller.repeat(reverse: true));
109+
}
106110
playPauseController.forward();
107111
}
108112
}

lib/views/widgets/animated_box/animated_box_widget.dart

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,65 @@ class AnimatedBoxWidget extends StatelessWidget {
1818
final theme = Theme.of(context).colorScheme;
1919

2020
return LayoutBuilder(builder: (context, constraints) {
21-
final boxSize = constraints.maxHeight / 4;
21+
final boxSize = min(constraints.maxWidth, constraints.maxHeight) / 4;
2222

2323
return AnimatedBuilder(
2424
animation: animation,
2525
child: ColoredBox(
2626
color: theme.primary,
27-
child: SizedBox(
28-
width: boxSize,
29-
height: boxSize,
30-
),
27+
child: SizedBox.square(dimension: boxSize),
3128
),
3229
builder: (context, child) {
33-
return Container(
34-
alignment: Alignment.center,
30+
return ColoredBox(
3531
color: theme.onPrimaryFixed,
36-
child: switch (animationType) {
37-
/// Rotate
38-
AnimationType.rotate =>
39-
Transform.rotate(angle: animation.value * pi, child: child),
32+
child: Align(
33+
alignment: Alignment.center,
34+
child: switch (animationType) {
35+
/// Rotate
36+
AnimationType.rotate =>
37+
Transform.rotate(angle: animation.value * pi, child: child),
4038

41-
/// Scale
42-
AnimationType.scale => Transform.scale(
43-
scale: animation.value,
44-
child: child,
45-
),
39+
/// Scale
40+
AnimationType.scale => Transform.scale(
41+
scale: animation.value,
42+
child: child,
43+
),
4644

47-
/// Translate x
48-
AnimationType.translateX => Transform.translate(
49-
offset: Tween(
50-
begin: Offset((-constraints.maxWidth + boxSize) / 2, 0),
51-
end: Offset((constraints.maxHeight - boxSize) / 2, 0),
52-
).transform(animation.value),
53-
child: child,
54-
),
45+
/// Translate x
46+
AnimationType.translateX => Transform.translate(
47+
offset: Tween(
48+
begin: Offset((-constraints.maxWidth + boxSize) / 2, 0),
49+
end: Offset((constraints.maxHeight - boxSize) / 2, 0),
50+
).transform(animation.value),
51+
child: child,
52+
),
5553

56-
/// Translate Y
57-
AnimationType.translateY => Transform.translate(
58-
offset: Tween(
59-
begin: Offset(0, (-constraints.maxHeight + boxSize) / 2),
60-
end: Offset(0, (constraints.maxHeight - boxSize) / 2),
61-
).transform(animation.value),
62-
child: child,
63-
),
54+
/// Translate Y
55+
AnimationType.translateY => Transform.translate(
56+
offset: Tween(
57+
begin: Offset(0, (-constraints.maxHeight + boxSize) / 2),
58+
end: Offset(0, (constraints.maxHeight - boxSize) / 2),
59+
).transform(animation.value),
60+
child: child,
61+
),
6462

65-
/// Fade
66-
AnimationType.fade => Opacity(
67-
// accept values between 0 and 1
68-
opacity: max(0, min(1, animation.value)),
69-
child: child,
70-
),
63+
/// Fade
64+
AnimationType.fade => AnimatedOpacity(
65+
duration: const Duration(milliseconds: 100),
66+
opacity: animation.value.clamp(0.0, 1.0),
67+
child: child,
68+
),
7169

72-
/// Flip
73-
AnimationType.flip => Transform(
74-
alignment: FractionalOffset.center,
75-
transform: Matrix4.identity()
76-
..setEntry(2, 1, 0.0015)
77-
..rotateY(animation.value * pi),
78-
child: child,
79-
),
80-
},
70+
/// Flip
71+
AnimationType.flip => Transform(
72+
alignment: FractionalOffset.center,
73+
transform: Matrix4.identity()
74+
..setEntry(2, 1, 0.0002)
75+
..rotateY(animation.value * pi),
76+
child: child,
77+
),
78+
},
79+
),
8180
);
8281
},
8382
);

0 commit comments

Comments
 (0)