text
stringlengths
1
372
you can use your stateful object as the vsync by adding
SingleTickerProviderStateMixin to the class definition.
you can see an example of this in animate1 on GitHub.
info note
in some cases, a position might exceed the AnimationController’s
0.0-1.0 range. for example, the fling() function
allows you to provide velocity, force, and position
(via the force object). the position can be anything and
so can be outside of the 0.0 to 1.0 range.
a CurvedAnimation can also exceed the 0.0 to 1.0 range,
even if the AnimationController doesn’t.
depending on the curve selected, the output of
the CurvedAnimation can have a wider range than the input.
for example, elastic curves such as Curves.elasticIn
significantly overshoots or undershoots the default range.
<topic_end>
<topic_start>
tween
by default, the AnimationController object ranges from 0.0 to 1.0.
if you need a different range or a different data type, you can use a
tween to configure an animation to interpolate to a
different range or data type. for example, the
following tween goes from -200.0 to 0.0:
<code_start>
tween = tween<double>(begin: -200, end: 0);
<code_end>
a tween is a stateless object that takes only begin and end.
the sole job of a tween is to define a mapping from an
input range to an output range. the input range is commonly
0.0 to 1.0, but that’s not a requirement.
a tween inherits from Animatable<T>, not from Animation<T>.
an animatable, like animation, doesn’t have to output double.
for example, ColorTween specifies a progression between two colors.
<code_start>
colorTween = ColorTween(begin: colors.transparent, end: colors.black54);
<code_end>
a tween object doesn’t store any state. instead, it provides the
evaluate(Animation<double> animation) method that uses the
transform function to map the current value of the animation
(between 0.0 and 1.0), to the actual animation value.
the current value of the animation object can be found in the
.value method. the evaluate function also performs some housekeeping,
such as ensuring that begin and end are returned when the
animation values are 0.0 and 1.0, respectively.
<topic_end>
<topic_start>
tween.animate
to use a tween object, call animate() on the tween,
passing in the controller object. for example,
the following code generates the
integer values from 0 to 255 over the course of 500 ms.
<code_start>
AnimationController controller = AnimationController(
duration: const duration(milliseconds: 500), vsync: this);
animation<int> alpha = IntTween(begin: 0, end: 255).animate(controller);
<code_end>
info note
the animate() method returns an animation,
not an animatable.
the following example shows a controller, a curve, and a tween:
<code_start>
AnimationController controller = AnimationController(
duration: const duration(milliseconds: 500), vsync: this);
final animation<double> curve =
CurvedAnimation(parent: controller, curve: Curves.easeOut);
animation<int> alpha = IntTween(begin: 0, end: 255).animate(curve);
<code_end>
<topic_end>
<topic_start>
animation notifications
an animation object can have listeners and StatusListeners,
defined with addListener() and addStatusListener().
a listener is called whenever the value of the animation changes.
the most common behavior of a listener is to call setState()
to cause a rebuild. a StatusListener is called when an animation begins,
ends, moves forward, or moves reverse, as defined by AnimationStatus.
the next section has an example of the addListener() method,
and monitoring the progress of the animation shows an
example of addStatusListener().
<topic_end>
<topic_start>
animation examples
this section walks you through 5 animation examples.
each section provides a link to the source code for that example.
<topic_end>
<topic_start>
rendering animations
<topic_end>
<topic_start>
what's the point?
so far you’ve learned how to generate a sequence of numbers over time.
nothing has been rendered to the screen. to render with an
animation object, store the animation object as a
member of your widget, then use its value to decide how to draw.
consider the following app that draws the flutter logo without animation:
<code_start>
import 'package:flutter/material.dart';
void main() => runApp(const LogoApp());
class LogoApp extends StatefulWidget {
const LogoApp({super.key});