text stringlengths 1 474 |
|---|
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}); |
@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: animate0The 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: animate1The 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 AnimatedWidget |
<topic_end> |
<topic_start>What's the point? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.