Skip to content

Commit 3348c2f

Browse files
authored
Add helpful comments to the animation samples (flutter#136)
1 parent 6cda477 commit 3348c2f

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

animations/lib/src/basics/01_animated_container.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import 'dart:math';
66

77
import 'package:flutter/material.dart';
88

9-
const _duration = Duration(milliseconds: 400);
10-
119
double generateBorderRadius() => Random().nextDouble() * 64;
1210
double generateMargin() => Random().nextDouble() * 64;
1311
Color generateColor() => Color(0xFFFFFFFF & Random().nextInt(0xFFFFFFFF));
@@ -39,13 +37,20 @@ class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
3937
}
4038

4139
Widget build(BuildContext context) {
40+
// This widget is built using an AnimatedContainer, one of the easiest to use
41+
// animated Widgets. Whenever the AnimatedContainer's properties, such as decoration,
42+
// change, it will handle animating from the previous value to the new value. You can
43+
// specify both a Duration and a Curve for the animation.
44+
// This Widget is useful for designing animated interfaces that just need to change
45+
// the properties of a container. For example, you could use this to design expanding
46+
// and shrinking cards.
4247
return Scaffold(
4348
appBar: AppBar(),
4449
body: Center(
4550
child: Column(
4651
children: [
4752
Padding(
48-
padding: const EdgeInsets.all(8.0),
53+
padding: EdgeInsets.all(8.0),
4954
child: SizedBox(
5055
width: 128,
5156
height: 128,
@@ -55,7 +60,7 @@ class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
5560
color: color,
5661
borderRadius: BorderRadius.circular(borderRadius),
5762
),
58-
duration: _duration,
63+
duration: Duration(milliseconds: 400),
5964
),
6065
),
6166
),

animations/lib/src/basics/03_animation_controller.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class AnimationControllerDemo extends StatefulWidget {
1414

1515
class _AnimationControllerDemoState extends State<AnimationControllerDemo>
1616
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+
1722
static const Duration _duration = Duration(seconds: 1);
1823
AnimationController controller;
1924

@@ -22,20 +27,31 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
2227
super.initState();
2328

2429
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.
2537
..addListener(() {
26-
// Force build() to be called again
2738
setState(() {});
2839
});
2940
}
3041

3142
@override
3243
void dispose() {
44+
// AnimationController is a stateful resource that needs to be disposed when
45+
// this State gets disposed.
3346
controller.dispose();
3447
super.dispose();
3548
}
3649

3750
@override
3851
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.
3955
return Scaffold(
4056
appBar: AppBar(),
4157
body: Center(
@@ -46,7 +62,8 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
4662
constraints: BoxConstraints(maxWidth: 200),
4763
child: Text(
4864
'${controller.value.toStringAsFixed(2)}',
49-
style: Theme.of(context).textTheme.display3,
65+
style: Theme.of(context).textTheme.display2,
66+
textScaleFactor: 1 + controller.value,
5067
),
5168
),
5269
RaisedButton(

animations/lib/src/basics/05_animated_builder.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class _AnimatedBuilderDemoState extends State<AnimatedBuilderDemo>
3535
return Scaffold(
3636
appBar: AppBar(),
3737
body: Center(
38+
// AnimatedBuilder handles listening to a given animation and calling the builder
39+
// whenever the value of the animation change. This can be useful when a Widget
40+
// tree contains some animated and non-animated elements, as only the subtree
41+
// created by the builder needs to be re-built when the animation changes.
3842
child: AnimatedBuilder(
3943
animation: animation,
4044
builder: (context, child) {
@@ -50,6 +54,10 @@ class _AnimatedBuilderDemoState extends State<AnimatedBuilderDemo>
5054
},
5155
);
5256
},
57+
// AnimatedBuilder can also accept a pre-built child Widget which is useful
58+
// if there is a non-animated Widget contained within the animated widget.
59+
// This can improve performance since this widget doesn't need to be rebuilt
60+
// when the animation changes.
5361
child: Text(
5462
'Change Color',
5563
style: TextStyle(color: Colors.white),

0 commit comments

Comments
 (0)