/
common.dart
72 lines (63 loc) · 2.03 KB
/
common.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
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'framework.dart';
extension CommonHelpers on LifeMixin {
StateRef<int, SW> useAnimInt<SW extends StatefulWidget>(
StateRef<Animation<int>, SW> animation) {
final animationValue = init(get(animation).value);
animation.watch((newAnimation) {
newAnimation.addListener(() {
set(animationValue, get(animation).value);
});
});
return animationValue;
}
StateRef<Animation<int>, SW> createAnimInt<SW extends StatefulWidget>(
TickerProvider ticker,
StateRef<int, SW> value, {
Duration duration,
Curve curve,
}) {
final animationController =
createAnimationController(ticker, duration: duration);
final animation = init<Animation<int>>(
AlwaysStoppedAnimation(get(value)),
);
animation.onDidUpdateWidget((animation, _) {
final newAnimation = get(animationController).drive(
IntTween(begin: animation.value, end: get(value)).chain(
CurveTween(curve: curve),
),
);
get(animationController).forward(from: 0);
return newAnimation;
});
value.watch((_) {
final newAnimation = get(animationController).drive(
IntTween(begin: get(animation).value, end: get(value)).chain(
CurveTween(curve: curve),
),
);
get(animationController).forward(from: 0);
set(animation, newAnimation);
});
return animation;
}
StateRef<AnimationController, SW>
createAnimationController<SW extends StatefulWidget>(
TickerProvider ticker,
{Duration duration}) {
final animationController =
init(AnimationController(duration: duration, vsync: ticker));
animationController.onDispose((controller) => controller.dispose());
return animationController;
}
StateRef<T, SW> initListenable<T, SW extends StatefulWidget>(
ValueListenable<T> vl) {
final state = init(vl.value);
vl.addListener(() {
set(state, vl.value);
});
return state;
}
}