Skip to content

Commit 946dfb9

Browse files
committed
update support simple_animation 2.2.2
1 parent 6e6a265 commit 946dfb9

File tree

8 files changed

+197
-98
lines changed

8 files changed

+197
-98
lines changed

ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ DEPENDENCIES:
4949
- webview_flutter (from `.symlinks/plugins/webview_flutter/ios`)
5050

5151
SPEC REPOS:
52-
https://github.com/cocoapods/specs.git:
52+
https://github.com/CocoaPods/Specs.git:
5353
- FMDB
5454
- Reachability
5555
- Toast
@@ -102,4 +102,4 @@ SPEC CHECKSUMS:
102102

103103
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
104104

105-
COCOAPODS: 1.7.5
105+
COCOAPODS: 1.8.0

lib/widget/animated_background.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import 'package:flutter/material.dart';
2-
import 'package:simple_animations/simple_animations/controlled_animation.dart';
3-
import 'package:simple_animations/simple_animations/multi_track_tween.dart';
2+
import 'package:simple_animations/simple_animations.dart';
3+
import 'package:supercharged/supercharged.dart';
4+
5+
enum _ColorTween { color1, color2 }
46

57
class AnimatedBackground extends StatelessWidget {
68
@override
79
Widget build(BuildContext context) {
8-
final tween = MultiTrackTween([
9-
Track("color1").add(Duration(seconds: 3),
10-
ColorTween(begin: Color(0xffD38312), end: Colors.lightBlue.shade900)),
11-
Track("color2").add(Duration(seconds: 3),
12-
ColorTween(begin: Color(0xffA83279), end: Colors.blue.shade600))
13-
]);
10+
final tween = MultiTween<_ColorTween>()
11+
..add(
12+
_ColorTween.color1,
13+
Color(0xffD38312).tweenTo(Colors.lightBlue.shade900),
14+
3.seconds,
15+
)
16+
..add(
17+
_ColorTween.color2,
18+
Color(0xffA83279).tweenTo(Colors.blue.shade600),
19+
3.seconds,
20+
);
1421

15-
return ControlledAnimation(
16-
playback: Playback.MIRROR,
22+
return MirrorAnimation<MultiTweenValues<_ColorTween>>(
1723
tween: tween,
1824
duration: tween.duration,
19-
builder: (context, animation) {
25+
builder: (context, child, value) {
2026
return Container(
2127
decoration: BoxDecoration(
2228
gradient: LinearGradient(
2329
begin: Alignment.topCenter,
2430
end: Alignment.bottomCenter,
25-
colors: [animation["color1"], animation["color2"]])),
31+
colors: [
32+
value.get<Color>(_ColorTween.color1),
33+
value.get<Color>(_ColorTween.color2)
34+
])),
2635
);
2736
},
2837
);
2938
}
30-
}
39+
}

lib/widget/mole_widget.dart

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import 'dart:math';
44
import 'package:flutter/cupertino.dart';
55
import 'package:flutter/material.dart';
66
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';
109

1110
class Mole extends StatefulWidget {
1211
@override
@@ -15,11 +14,14 @@ class Mole extends StatefulWidget {
1514

1615
class _MoleState extends State<Mole> {
1716
final List<MoleParticle> particles = [];
17+
bool _moleIsVisible = false;
1818

1919
@override
2020
void initState() {
2121
_restartMole();
22-
_hitMole(Duration(milliseconds: 1000));
22+
Future.delayed(1200.milliseconds, () {
23+
_hitMole();
24+
});
2325
super.initState();
2426
}
2527

@@ -32,76 +34,109 @@ class _MoleState extends State<Mole> {
3234
);
3335
}
3436

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) {
3942
return Stack(
4043
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+
],
4249
);
4350
},
4451
);
4552
}
4653

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()));
4964
}
5065

5166
void _restartMole() async {
52-
var respawnTime = Duration(milliseconds: 2000 + Random().nextInt(8000));
67+
var respawnTime = (2000 + Random().nextInt(8000)).milliseconds;
5368
await Future.delayed(respawnTime);
69+
_setMoleVisible(true);
5470

55-
var timeVisible = Duration(milliseconds: 500 + Random().nextInt(1500));
71+
var timeVisible = (500 + Random().nextInt(1500)).milliseconds;
5672
await Future.delayed(timeVisible);
73+
_setMoleVisible(false);
5774

58-
if (mounted) {
59-
_restartMole();
60-
}
75+
_restartMole();
6176
}
6277

63-
_manageParticleLifecycle(Duration time) {
78+
_manageParticleLifecycle() {
6479
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;
6687
});
6788
}
89+
90+
@override
91+
void setState(fn) {
92+
if (mounted) {
93+
super.setState(fn);
94+
}
95+
}
6896
}
6997

98+
enum _MoleProps { x, y, scale }
99+
70100
class MoleParticle {
71-
Animatable tween;
72-
AnimationProgress progress;
101+
Animatable<MultiTweenValues<_MoleProps>> tween;
102+
Duration startTime;
103+
final duration = 600.milliseconds;
73104

74-
MoleParticle(Duration time) {
105+
MoleParticle() {
75106
final random = Random();
76107
final x = (100 + 200) * random.nextDouble() * (random.nextBool() ? 1 : -1);
77108
final y = (100 + 200) * random.nextDouble() * (random.nextBool() ? 1 : -1);
78109

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();
86116
}
87117

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();
91121
return Positioned(
92-
left: animation["x"],
93-
top: animation["y"],
122+
left: values.get(_MoleProps.x),
123+
top: values.get(_MoleProps.y),
94124
child: Transform.scale(
95-
scale: animation["scale"],
125+
scale: values.get(_MoleProps.scale),
96126
child: Container(
97127
width: 100,
98128
height: 100,
99129
decoration: BoxDecoration(
100-
color: GSYColors.primaryValue
101-
.withAlpha(alpha),
130+
color: GSYColors.primaryValue.withAlpha(alpha),
102131
borderRadius: BorderRadius.circular(50)),
103132
),
104133
),
105134
);
106135
}
136+
137+
double progress() {
138+
return ((DateTime.now().duration() - startTime) / duration)
139+
.clamp(0.0, 1.0)
140+
.toDouble();
141+
}
107142
}
Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
import 'dart:math';
22

33
import 'package:flutter/material.dart';
4-
import 'package:simple_animations/simple_animations/animation_progress.dart';
5-
import 'package:simple_animations/simple_animations/multi_track_tween.dart';
4+
import 'package:simple_animations/simple_animations.dart';
5+
import 'package:supercharged/supercharged.dart';
6+
7+
8+
enum ParticleOffsetProps { x, y }
69

710
class ParticleModel {
8-
Animatable tween;
11+
MultiTween<ParticleOffsetProps> tween;
912
double size;
10-
AnimationProgress animationProgress;
13+
Duration duration;
14+
Duration startTime;
1115
Random random;
12-
int defaultMilliseconds;
1316

14-
ParticleModel(this.random, {this.defaultMilliseconds = 500}) {
15-
restart();
17+
ParticleModel(this.random) {
18+
_restart();
19+
_shuffle();
1620
}
1721

18-
restart({Duration time = Duration.zero}) {
22+
_restart({Duration time = Duration.zero}) {
1923
final startPosition = Offset(-0.2 + 1.4 * random.nextDouble(), 1.2);
20-
2124
final endPosition = Offset(-0.2 + 1.4 * random.nextDouble(), -0.2);
2225

23-
final duration =
24-
Duration(milliseconds: defaultMilliseconds + random.nextInt(1000));
25-
26-
tween = MultiTrackTween([
27-
Track("x").add(
28-
duration, Tween(begin: startPosition.dx, end: endPosition.dx),
29-
curve: Curves.easeInOutSine),
30-
Track("y").add(
31-
duration, Tween(begin: startPosition.dy, end: endPosition.dy),
32-
curve: Curves.easeIn),
33-
]);
34-
animationProgress = AnimationProgress(duration: duration, startTime: time);
26+
tween = MultiTween<ParticleOffsetProps>()
27+
..add(ParticleOffsetProps.x, startPosition.dx.tweenTo(endPosition.dx))
28+
..add(ParticleOffsetProps.y, startPosition.dy.tweenTo(endPosition.dy));
29+
30+
duration = 3000.milliseconds + random.nextInt(6000).milliseconds;
31+
startTime = DateTime.now().duration();
3532
size = 0.2 + random.nextDouble() * 0.4;
3633
}
3734

38-
maintainRestart(Duration time) {
39-
if (animationProgress.progress(time) == 1.0) {
40-
restart(time: time);
35+
void _shuffle() {
36+
startTime -= (this.random.nextDouble() * duration.inMilliseconds)
37+
.round()
38+
.milliseconds;
39+
}
40+
41+
checkIfParticleNeedsToBeRestarted() {
42+
if (progress() == 1.0) {
43+
_restart();
4144
}
4245
}
46+
47+
double progress() {
48+
return ((DateTime.now().duration() - startTime) / duration)
49+
.clamp(0.0, 1.0)
50+
.toDouble();
51+
}
4352
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import 'package:flutter/material.dart';
22
import 'package:gsy_github_app_flutter/widget/particle/particle_model.dart';
3+
import 'package:simple_animations/simple_animations.dart';
34

45
class ParticlePainter extends CustomPainter {
56
List<ParticleModel> particles;
6-
Duration time;
7-
Color color;
87

9-
ParticlePainter(this.particles, this.time, {this.color = Colors.white});
8+
ParticlePainter(this.particles);
109

1110
@override
1211
void paint(Canvas canvas, Size size) {
13-
final paint = Paint()..color = color.withAlpha(50);
12+
final paint = Paint()..color = Colors.white.withAlpha(50);
13+
1414
particles.forEach((particle) {
15-
var progress = particle.animationProgress.progress(time);
16-
final animation = particle.tween.transform(progress);
17-
final position =
18-
Offset(animation["x"] * size.width, animation["y"] * size.height);
15+
final progress = particle.progress();
16+
final MultiTweenValues<ParticleOffsetProps> animation =
17+
particle.tween.transform(progress);
18+
final position = Offset(
19+
animation.get<double>(ParticleOffsetProps.x) * size.width,
20+
animation.get<double>(ParticleOffsetProps.y) * size.height,
21+
);
1922
canvas.drawCircle(position, size.width * 0.2 * particle.size, paint);
2023
});
2124
}
2225

2326
@override
2427
bool shouldRepaint(CustomPainter oldDelegate) => true;
25-
}
28+
}

0 commit comments

Comments
 (0)