1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-30 16:23:23 +00:00

Add helpful comments to the animation samples (#136)

This commit is contained in:
Thea Flowers
2019-09-05 09:46:17 -07:00
committed by GitHub
parent 6cda477f20
commit 3348c2f2dd
3 changed files with 36 additions and 6 deletions

View File

@@ -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(