-
Notifications
You must be signed in to change notification settings - Fork 0
/
flying_widgets_animation.dart
128 lines (112 loc) · 3.17 KB
/
flying_widgets_animation.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
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
class FlyingWidgetsAnimation extends StatefulWidget {
final int numberOfItems;
final Widget? animatedItem;
final Widget? child;
final double? size;
final Color? background;
const FlyingWidgetsAnimation({
required this.numberOfItems,
this.animatedItem,
this.child,
this.size,
this.background,
super.key,
});
@override
State<FlyingWidgetsAnimation> createState() => _FlyingWidgetsAnimationState();
}
class _FlyingWidgetsAnimationState extends State<FlyingWidgetsAnimation>
with TickerProviderStateMixin {
List<AnimatedObject> animatedObjects = [];
Timer? timer;
final int period = 3;
void _startAnimation() {
final Duration interval =
Duration(milliseconds: (period / widget.numberOfItems * 1000).toInt());
timer = Timer.periodic(interval, (Timer t) {
if (animatedObjects.length < widget.numberOfItems) {
var newAnimation = _createAnimatedObject();
setState(() {
animatedObjects.add(newAnimation);
});
} else {
timer?.cancel();
}
});
}
AnimatedObject _createAnimatedObject() {
var controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
);
var animation = _createRandomAnimation(controller);
controller.forward();
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.forward(from: 0.0);
}
});
return AnimatedObject(controller, animation);
}
Animation<Offset> _createRandomAnimation(AnimationController controller) {
return Tween<Offset>(
begin: Offset(-Random().nextDouble(), -Random().nextDouble()),
end: Offset(Random().nextDouble() + 1, Random().nextDouble() + 1),
).animate(
CurvedAnimation(
parent: controller,
curve: Curves.linear,
),
);
}
List<SlideTransition> _animationList() {
return animatedObjects.map((animatedObject) {
return SlideTransition(
position: animatedObject.animation,
child: Container(
alignment: Alignment.center,
child: widget.animatedItem ??
Icon(
Icons.sentiment_satisfied_alt,
size: widget.size ?? 100,
color: Color.fromARGB(
255,
Random().nextInt(255),
Random().nextInt(255),
Random().nextInt(255),
),
),
),
);
}).toList();
}
@override
void initState() {
super.initState();
_startAnimation();
}
@override
Widget build(BuildContext context) {
return Container(
color: widget.background ?? Colors.white,
child:
Stack(children: [..._animationList(), Center(child: widget.child)]),
);
}
@override
void dispose() {
animatedObjects.forEach((animatedObject) {
animatedObject.controller.dispose();
});
timer?.cancel();
super.dispose();
}
}
class AnimatedObject {
final AnimationController controller;
final Animation<Offset> animation;
AnimatedObject(this.controller, this.animation);
}