@@ -4,9 +4,8 @@ import 'dart:math';
4
4
import 'package:flutter/cupertino.dart' ;
5
5
import 'package:flutter/material.dart' ;
6
6
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart' ;
7
- import 'package:simple_animations/simple_animations/animation_progress.dart' ;
8
- import 'package:simple_animations/simple_animations/multi_track_tween.dart' ;
9
- import 'package:simple_animations/simple_animations/rendering.dart' ;
7
+ import 'package:simple_animations/simple_animations.dart' ;
8
+ import 'package:supercharged/supercharged.dart' ;
10
9
11
10
class Mole extends StatefulWidget {
12
11
@override
@@ -15,11 +14,14 @@ class Mole extends StatefulWidget {
15
14
16
15
class _MoleState extends State <Mole > {
17
16
final List <MoleParticle > particles = [];
17
+ bool _moleIsVisible = false ;
18
18
19
19
@override
20
20
void initState () {
21
21
_restartMole ();
22
- _hitMole (Duration (milliseconds: 1000 ));
22
+ Future .delayed (1200. milliseconds, () {
23
+ _hitMole ();
24
+ });
23
25
super .initState ();
24
26
}
25
27
@@ -32,76 +34,109 @@ class _MoleState extends State<Mole> {
32
34
);
33
35
}
34
36
35
- Rendering _buildMole () {
36
- return Rendering (
37
- onTick: (time) => _manageParticleLifecycle (time),
38
- builder: (context, time) {
37
+ Widget _buildMole () {
38
+ _manageParticleLifecycle ();
39
+ return LoopAnimation <int >(
40
+ tween: ConstantTween (1 ),
41
+ builder: (context, child, value) {
39
42
return Stack (
40
43
overflow: Overflow .visible,
41
- children: [...particles.map ((it) => it.buildWidget (time))],
44
+ children: [
45
+ if (_moleIsVisible)
46
+ GestureDetector (onTap: () => _hitMole (), child: _mole ()),
47
+ ...particles.map ((it) => it.buildWidget ())
48
+ ],
42
49
);
43
50
},
44
51
);
45
52
}
46
53
47
- _hitMole (Duration time) {
48
- Iterable .generate (50 ).forEach ((i) => particles.add (MoleParticle (time)));
54
+ Widget _mole () {
55
+ return Container (
56
+ decoration: BoxDecoration (
57
+ color: GSYColors .primaryValue,
58
+ borderRadius: BorderRadius .circular (50 )),
59
+ );
60
+ }
61
+
62
+ _hitMole () {
63
+ Iterable .generate (50 ).forEach ((i) => particles.add (MoleParticle ()));
49
64
}
50
65
51
66
void _restartMole () async {
52
- var respawnTime = Duration (milliseconds : 2000 + Random ().nextInt (8000 ));
67
+ var respawnTime = ( 2000 + Random ().nextInt (8000 )).milliseconds ;
53
68
await Future .delayed (respawnTime);
69
+ _setMoleVisible (true );
54
70
55
- var timeVisible = Duration (milliseconds : 500 + Random ().nextInt (1500 ));
71
+ var timeVisible = ( 500 + Random ().nextInt (1500 )).milliseconds ;
56
72
await Future .delayed (timeVisible);
73
+ _setMoleVisible (false );
57
74
58
- if (mounted) {
59
- _restartMole ();
60
- }
75
+ _restartMole ();
61
76
}
62
77
63
- _manageParticleLifecycle (Duration time ) {
78
+ _manageParticleLifecycle () {
64
79
particles.removeWhere ((particle) {
65
- return particle.progress.progress (time) == 1 ;
80
+ return particle.progress () == 1 ;
81
+ });
82
+ }
83
+
84
+ void _setMoleVisible (bool visible) {
85
+ setState (() {
86
+ _moleIsVisible = visible;
66
87
});
67
88
}
89
+
90
+ @override
91
+ void setState (fn) {
92
+ if (mounted) {
93
+ super .setState (fn);
94
+ }
95
+ }
68
96
}
69
97
98
+ enum _MoleProps { x, y, scale }
99
+
70
100
class MoleParticle {
71
- Animatable tween;
72
- AnimationProgress progress;
101
+ Animatable <MultiTweenValues <_MoleProps >> tween;
102
+ Duration startTime;
103
+ final duration = 600. milliseconds;
73
104
74
- MoleParticle (Duration time ) {
105
+ MoleParticle () {
75
106
final random = Random ();
76
107
final x = (100 + 200 ) * random.nextDouble () * (random.nextBool () ? 1 : - 1 );
77
108
final y = (100 + 200 ) * random.nextDouble () * (random.nextBool () ? 1 : - 1 );
78
109
79
- tween = MultiTrackTween ([
80
- Track ("x" ).add (Duration (seconds: 1 ), Tween (begin: 0.0 , end: x)),
81
- Track ("y" ).add (Duration (seconds: 1 ), Tween (begin: 0.0 , end: y)),
82
- Track ("scale" ).add (Duration (seconds: 1 ), Tween (begin: 1.0 , end: 0.0 ))
83
- ]);
84
- progress = AnimationProgress (
85
- startTime: time, duration: Duration (milliseconds: 600 ));
110
+ tween = MultiTween <_MoleProps >()
111
+ ..add (_MoleProps .x, 0.0 .tweenTo (x))
112
+ ..add (_MoleProps .y, 0.0 .tweenTo (y))
113
+ ..add (_MoleProps .scale, 1.0 .tweenTo (0.0 ));
114
+
115
+ startTime = DateTime .now ().duration ();
86
116
}
87
117
88
- buildWidget (Duration time ) {
89
- final animation = tween.transform (progress. progress (time ));
90
- var alpha = (255 * progress. progress (time )).toInt ();
118
+ Widget buildWidget () {
119
+ final MultiTweenValues < _MoleProps > values = tween.transform (progress ( ));
120
+ var alpha = (255 * progress ( )).toInt ();
91
121
return Positioned (
92
- left: animation[ "x" ] ,
93
- top: animation[ "y" ] ,
122
+ left: values. get ( _MoleProps .x) ,
123
+ top: values. get ( _MoleProps .y) ,
94
124
child: Transform .scale (
95
- scale: animation[ " scale" ] ,
125
+ scale: values. get ( _MoleProps . scale) ,
96
126
child: Container (
97
127
width: 100 ,
98
128
height: 100 ,
99
129
decoration: BoxDecoration (
100
- color: GSYColors .primaryValue
101
- .withAlpha (alpha),
130
+ color: GSYColors .primaryValue.withAlpha (alpha),
102
131
borderRadius: BorderRadius .circular (50 )),
103
132
),
104
133
),
105
134
);
106
135
}
136
+
137
+ double progress () {
138
+ return ((DateTime .now ().duration () - startTime) / duration)
139
+ .clamp (0.0 , 1.0 )
140
+ .toDouble ();
141
+ }
107
142
}
0 commit comments