-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathblob_animator.dart
51 lines (44 loc) · 1.45 KB
/
blob_animator.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
import 'package:flutter/material.dart';
class BlobAnimator {
List<Offset> pathPoints;
AnimationController animationController;
List<List<Tween>> tweens = [];
List<List<Animation>> anims = [];
final Curve? animationCurve;
BlobAnimator({required this.pathPoints, required this.animationController, this.animationCurve = Curves.linear});
init(Function(List<Offset>) callback) {
Animation<double> animation = CurvedAnimation(
parent: animationController,
curve: animationCurve!,
);
pathPoints.asMap().forEach((i, p) {
tweens.insert(i, []);
anims.insert(i, []);
tweens[i].add(Tween(begin: 0, end: 0));
anims[i].add(tweens[i][0].animate(animation));
tweens[i].add(Tween(begin: 0, end: 0));
anims[i].add(tweens[i][1].animate(animation));
});
animationController.addListener(() {
List<Offset> transitionPoints = [];
anims.asMap().forEach((i, value) {
transitionPoints.add(Offset(
anims[i][0].value.toDouble(),
anims[i][1].value.toDouble(),
));
});
callback(transitionPoints);
});
morphTo(pathPoints);
}
morphTo(List<Offset> newPathPoints) {
tweens.asMap().forEach((i, tween) {
tween[0].begin = tween[0].end;
tween[0].end = newPathPoints[i].dx + 5;
tween[1].begin = tween[1].end;
tween[1].end = newPathPoints[i].dy + 5;
});
animationController.reset();
animationController.forward();
}
}