text
stringlengths
1
474
}<code_end>
<topic_end>
<topic_start>Animations API overview
The animation system in Flutter is based on typed
Animation objects. Widgets can either
incorporate these animations in their build
functions directly by reading their current value and listening to their
state changes or they can use the animations as the basis of more elaborate
animations that they pass along to other widgets.<topic_end>
<topic_start>
Animation
The primary building block of the animation system is the
Animation class. An animation represents a value
of a specific type that can change over the lifetime of
the animation. Most widgets that perform an animation
receive an Animation object as a parameter,
from which they read the current value of the animation
and to which they listen for changes to that value.<topic_end>
<topic_start>
addListener
Whenever the animation’s value changes,
the animation notifies all the listeners added with
addListener. Typically, a State
object that listens to an animation calls
setState on itself in its listener callback
to notify the widget system that it needs to
rebuild with the new value of the animation.This pattern is so common that there are two widgets
that help widgets rebuild when animations change value:
AnimatedWidget and AnimatedBuilder.
The first, AnimatedWidget, is most useful for
stateless animated widgets. To use AnimatedWidget,
simply subclass it and implement the build function.
The second, AnimatedBuilder, is useful for more complex widgets
that wish to include an animation as part of a larger build function.
To use AnimatedBuilder, simply construct the widget
and pass it a builder function.<topic_end>
<topic_start>
addStatusListener
Animations also provide an AnimationStatus,
which indicates how the animation will evolve over time.
Whenever the animation’s status changes,
the animation notifies all the listeners added with
addStatusListener. Typically, animations start
out in the dismissed status, which means they’re
at the beginning of their range. For example,
animations that progress from 0.0 to 1.0
will be dismissed when their value is 0.0.
An animation might then run forward (from 0.0 to 1.0)
or perhaps in reverse (from 1.0 to 0.0).
Eventually, if the animation reaches the end of its range
(1.0), the animation reaches the completed status.<topic_end>
<topic_start>
Animation­Controller
To create an animation, first create an AnimationController.
As well as being an animation itself, an AnimationController
lets you control the animation. For example,
you can tell the controller to play the animation
forward or stop the animation.
You can also fling animations,
which uses a physical simulation, such as a spring,
to drive the animation.Once you’ve created an animation controller,
you can start building other animations based on it.
For example, you can create a ReverseAnimation
that mirrors the original animation but runs in the
opposite direction (from 1.0 to 0.0).
Similarly, you can create a CurvedAnimation
whose value is adjusted by a Curve.<topic_end>
<topic_start>
Tweens
To animate beyond the 0.0 to 1.0 interval, you can use a
Tween<T>, which interpolates between its
begin and end values. Many types have specific
Tween subclasses that provide type-specific interpolation.
For example, ColorTween interpolates between colors and
RectTween interpolates between rects.
You can define your own interpolations by creating
your own subclass of Tween and overriding its
lerp function.By itself, a tween just defines how to interpolate
between two values. To get a concrete value for the
current frame of an animation, you also need an
animation to determine the current state.
There are two ways to combine a tween
with an animation to get a concrete value:You can evaluate the tween at the current
value of an animation. This approach is most useful
for widgets that are already listening to the animation and hence
rebuilding whenever the animation changes value.You can animate the tween based on the animation.
Rather than returning a single value, the animate function
returns a new Animation that incorporates the tween.
This approach is most useful when you want to give the
newly created animation to another widget,
which can then read the current value that incorporates
the tween as well as listen for changes to the value.<topic_end>
<topic_start>
Architecture
Animations are actually built from a number of core building blocks.<topic_end>
<topic_start>
Scheduler
The SchedulerBinding is a singleton class
that exposes the Flutter scheduling primitives.For this discussion, the key primitive is the frame callbacks.
Each time a frame needs to be shown on the screen,