@@ -14,6 +14,11 @@ class AnimationControllerDemo extends StatefulWidget {
14
14
15
15
class _AnimationControllerDemoState extends State <AnimationControllerDemo >
16
16
with SingleTickerProviderStateMixin {
17
+ // Using the SingleTickerProviderStateMixin can ensure that our
18
+ // AnimationController only animates while the Widget is visible on the
19
+ // screen. This is a useful optimization that saves resources when the
20
+ // Widget is not visible.
21
+
17
22
static const Duration _duration = Duration (seconds: 1 );
18
23
AnimationController controller;
19
24
@@ -22,20 +27,31 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
22
27
super .initState ();
23
28
24
29
controller = AnimationController (vsync: this , duration: _duration)
30
+ // The Widget's build needs to be called every time the animation's
31
+ // value changes. So add an listener here that will call setState()
32
+ // and trigger the build() method to be called by the framework.
33
+ // If your Widget's build is relatively simple, this is a good option.
34
+ // However, if your build method returns a tree of child Widgets and
35
+ // most of them are not animated you should consider using
36
+ // AnimatedBuilder instead.
25
37
..addListener (() {
26
- // Force build() to be called again
27
38
setState (() {});
28
39
});
29
40
}
30
41
31
42
@override
32
43
void dispose () {
44
+ // AnimationController is a stateful resource that needs to be disposed when
45
+ // this State gets disposed.
33
46
controller.dispose ();
34
47
super .dispose ();
35
48
}
36
49
37
50
@override
38
51
Widget build (BuildContext context) {
52
+ // When building the widget you can read the AnimationController's value property
53
+ // when building child widgets. You can also check the status to see if the animation
54
+ // has completed.
39
55
return Scaffold (
40
56
appBar: AppBar (),
41
57
body: Center (
@@ -46,7 +62,8 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
46
62
constraints: BoxConstraints (maxWidth: 200 ),
47
63
child: Text (
48
64
'${controller .value .toStringAsFixed (2 )}' ,
49
- style: Theme .of (context).textTheme.display3,
65
+ style: Theme .of (context).textTheme.display2,
66
+ textScaleFactor: 1 + controller.value,
50
67
),
51
68
),
52
69
RaisedButton (
0 commit comments