|
|
|
@ -14,6 +14,11 @@ class AnimationControllerDemo extends StatefulWidget {
|
|
|
|
|
|
|
|
|
|
class _AnimationControllerDemoState extends State<AnimationControllerDemo>
|
|
|
|
|
with SingleTickerProviderStateMixin {
|
|
|
|
|
// Using the SingleTickerProviderStateMixin can ensure that our
|
|
|
|
|
// AnimationController only animates while the Widget is visible on the
|
|
|
|
|
// screen. This is a useful optimization that saves resources when the
|
|
|
|
|
// Widget is not visible.
|
|
|
|
|
|
|
|
|
|
static const Duration _duration = Duration(seconds: 1);
|
|
|
|
|
AnimationController controller;
|
|
|
|
|
|
|
|
|
@ -22,20 +27,31 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
|
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
|
|
controller = AnimationController(vsync: this, duration: _duration)
|
|
|
|
|
// The Widget's build needs to be called every time the animation's
|
|
|
|
|
// value changes. So add an listener here that will call setState()
|
|
|
|
|
// and trigger the build() method to be called by the framework.
|
|
|
|
|
// If your Widget's build is relatively simple, this is a good option.
|
|
|
|
|
// However, if your build method returns a tree of child Widgets and
|
|
|
|
|
// most of them are not animated you should consider using
|
|
|
|
|
// AnimatedBuilder instead.
|
|
|
|
|
..addListener(() {
|
|
|
|
|
// Force build() to be called again
|
|
|
|
|
setState(() {});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
// AnimationController is a stateful resource that needs to be disposed when
|
|
|
|
|
// this State gets disposed.
|
|
|
|
|
controller.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
// When building the widget you can read the AnimationController's value property
|
|
|
|
|
// when building child widgets. You can also check the status to see if the animation
|
|
|
|
|
// has completed.
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(),
|
|
|
|
|
body: Center(
|
|
|
|
@ -46,7 +62,8 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
|
|
|
|
|
constraints: BoxConstraints(maxWidth: 200),
|
|
|
|
|
child: Text(
|
|
|
|
|
'${controller.value.toStringAsFixed(2)}',
|
|
|
|
|
style: Theme.of(context).textTheme.display3,
|
|
|
|
|
style: Theme.of(context).textTheme.display2,
|
|
|
|
|
textScaleFactor: 1 + controller.value,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
RaisedButton(
|
|
|
|
|