text
stringlengths
1
372
@override
State<LogoApp> createState() => _LogoAppState();
}
class _LogoAppState extends State<LogoApp> {
@override
widget build(BuildContext context) {
return center(
child: container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: 300,
width: 300,
child: const FlutterLogo(),
),
);
}
}
<code_end>
app source: animate0
the following shows the same code modified to animate the
logo to grow from nothing to full size.
when defining an AnimationController, you must pass in a
vsync object. the vsync parameter is described in the
AnimationController section.
the changes from the non-animated example are highlighted:
app source: animate1
the addListener() function calls setState(),
so every time the animation generates a new number,
the current frame is marked dirty, which forces
build() to be called again. in build(),
the container changes size because its height and
width now use animation.value instead of a hardcoded value.
dispose of the controller when the state object is
discarded to prevent memory leaks.
with these few changes,
you’ve created your first animation in flutter!
dart language tricks:
you might not be familiar with dart’s cascade notation—the two
dots in ..addListener(). this syntax means that the addListener()
method is called with the return value from animate().
consider the following example:
this code is equivalent to:
to learn more about cascades,
check out cascade notation
in the dart language documentation.
<topic_end>
<topic_start>
simplifying with Animated­Widget
<topic_end>
<topic_start>
what's the point?
the AnimatedWidget base class allows you to separate out
the core widget code from the animation code.
AnimatedWidget doesn’t need to maintain a state
object to hold the animation. add the following AnimatedLogo class:
<code_start>
class AnimatedLogo extends AnimatedWidget {
const AnimatedLogo({super.key, required animation<double> animation})
: super(listenable: animation);
@override
widget build(BuildContext context) {
final animation = listenable as animation<double>;
return center(
child: container(
margin: const EdgeInsets.symmetric(vertical: 10),
height: animation.value,
width: animation.value,
child: const FlutterLogo(),
),
);
}
}
<code_end>
AnimatedLogo uses the current value of the animation
when drawing itself.
the LogoApp still manages the AnimationController and the tween,
and it passes the animation object to AnimatedLogo:
app source: animate2
<topic_end>
<topic_start>
monitoring the progress of the animation
<topic_end>
<topic_start>
what's the point?
it’s often helpful to know when an animation changes state,
such as finishing, moving forward, or reversing.
you can get notifications for this with addStatusListener().
the following code modifies the previous example so that
it listens for a state change and prints an update.
the highlighted line shows the change:
<code_start>
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
late animation<double> animation;
late AnimationController controller;
@override
void initState() {
super.initState();
controller =
AnimationController(duration: const duration(seconds: 2), vsync: this);
animation = tween<double>(begin: 0, end: 300).animate(controller)
..addStatusListener((status) => print('$status'));