Animations tutorial
This tutorial shows you how to build explicit animations in Flutter. After introducing some of the essential concepts, classes, and methods in the animation library, it walks you through 5 animation examples. The examples build on each other, introducing you to different aspects of the animation library.
The Flutter SDK also provides built-in explicit animations,
such as FadeTransition
, SizeTransition
,
and SlideTransition
. These simple animations are
triggered by setting a beginning and ending point.
They are simpler to implement
than custom explicit animations, which are described here.
Essential animation concepts and classes
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.
<double>
AnimationIn Flutter, an Animation
object knows nothing about what
is onscreen. An Animation
is an abstract class that
understands its current value and its state (completed or dismissed).
One of the more commonly used animation types is Animation<double>
.
An Animation
object sequentially generates
interpolated numbers between two values over a certain duration.
The output of an Animation
object might be linear,
a curve, a step function, or any other mapping you can devise.
Depending on how the Animation
object is controlled,
it could run in reverse, or even switch directions in the
middle.
Animations can also interpolate types other than double, such as
Animation<Color>
or Animation<Size>
.
An Animation
object has state. Its current value is
always available in the .value
member.
An Animation
object knows nothing about rendering or
build()
functions.
CurvedAnimation
A CurvedAnimation
defines the animation’s progress
as a non-linear curve.
animation = CurvedAnimation(parent: controller, curve: Curves.easeIn);
CurvedAnimation
and AnimationController
(described in the next section)
are both of type Animation<double>
, so you can pass them interchangeably.
The CurvedAnimation
wraps the object it’s modifying—you
don’t subclass AnimationController
to implement a curve.
AnimationController
AnimationController
is a special Animation
object that generates a new value whenever the hardware
is ready for a new frame. By default,
an AnimationController
linearly produces the numbers
from 0.0 to 1.0 during a given duration.
For example, this code creates an Animation
object,
but does not start it running:
controller = AnimationController(duration: const Duration(seconds: 2), vsync: this);
AnimationController
derives from Animation<double>
, so it can be used
wherever an Animation
object is needed. However, the AnimationController
has additional methods to control the animation. For example, you start
an animation with the .forward()
method. The generation of numbers is
tied to the screen refresh, so typically 60 numbers are generated per
second. After each number is generated, each Animation
object calls the
attached Listener
objects. To create a custom display list for each
child, see RepaintBoundary
.
When creating an AnimationController
, you pass it a vsync
argument.
The presence of vsync
prevents offscreen animations from consuming
unnecessary resources.
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.
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:
tween = Tween<double>(begin: -200, end: 0);
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.
colorTween = ColorTween(begin: Colors.transparent, end: Colors.black54);
A Tween
object does not store any state. Instead, it provides the
evaluate(Animation<double> animation)
method that
applies the mapping function to the current value of the animation.
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.
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.
AnimationController controller = AnimationController( duration: const Duration(milliseconds: 500), vsync: this); Animation<int> alpha = IntTween(begin: 0, end: 255).animate(controller);
The following example shows a controller, a curve, and a Tween
:
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);
Animation notifications
An Animation
object can have Listener
s and StatusListener
s,
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()
.
Animation examples
This section walks you through 5 animation examples. Each section provides a link to the source code for that example.
Rendering animations
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:
import 'package:flutter/material.dart'; void main() => runApp(const LogoApp()); class LogoApp extends StatefulWidget { const LogoApp({Key? key}) : super(key: key); @override _LogoAppState 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(), ), ); } }
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:
@@ -1,3 +1,4 @@
|
|
1
|
+
import 'package:flutter/animation.dart';
|
1
2
|
import 'package:flutter/material.dart';
|
2
3
|
void main() => runApp(const LogoApp());
|
@@ -9,16 +10,39 @@
|
|
9
10
|
_LogoAppState createState() => _LogoAppState();
|
10
11
|
}
|
11
|
-
class _LogoAppState extends State<LogoApp> {
|
12
|
+
class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
|
13
|
+
late Animation<double> animation;
|
14
|
+
late AnimationController controller;
|
15
|
+
|
16
|
+
@override
|
17
|
+
void initState() {
|
18
|
+
super.initState();
|
19
|
+
controller =
|
20
|
+
AnimationController(duration: const Duration(seconds: 2), vsync: this);
|
21
|
+
animation = Tween<double>(begin: 0, end: 300).animate(controller)
|
22
|
+
..addListener(() {
|
23
|
+
setState(() {
|
24
|
+
// The state that has changed here is the animation object’s value.
|
25
|
+
});
|
26
|
+
});
|
27
|
+
controller.forward();
|
28
|
+
}
|
29
|
+
|
12
30
|
@override
|
13
31
|
Widget build(BuildContext context) {
|
14
32
|
return Center(
|
15
33
|
child: Container(
|
16
34
|
margin: const EdgeInsets.symmetric(vertical: 10),
|
17
|
-
height:
|
18
|
-
width:
|
35
|
+
height: animation.value,
|
36
|
+
width: animation.value,
|
19
37
|
child: const FlutterLogo(),
|
20
38
|
),
|
21
39
|
);
|
22
40
|
}
|
41
|
+
|
42
|
+
@override
|
43
|
+
void dispose() {
|
44
|
+
controller.dispose();
|
45
|
+
super.dispose();
|
46
|
+
}
|
23
47
|
}
|
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!
Simplifying with AnimatedWidget
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:
class AnimatedLogo extends AnimatedWidget { const AnimatedLogo({Key? key, required Animation<double> animation}) : super(key: key, 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(), ), ); } }
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
:
@@ -1,11 +1,29 @@
|
|
1
1
|
import 'package:flutter/animation.dart';
|
2
2
|
import 'package:flutter/material.dart';
|
3
3
|
void main() => runApp(const LogoApp());
|
4
|
+
class AnimatedLogo extends AnimatedWidget {
|
5
|
+
const AnimatedLogo({Key? key, required Animation<double> animation})
|
6
|
+
: super(key: key, listenable: animation);
|
7
|
+
|
8
|
+
@override
|
9
|
+
Widget build(BuildContext context) {
|
10
|
+
final animation = listenable as Animation<double>;
|
11
|
+
return Center(
|
12
|
+
child: Container(
|
13
|
+
margin: const EdgeInsets.symmetric(vertical: 10),
|
14
|
+
height: animation.value,
|
15
|
+
width: animation.value,
|
16
|
+
child: const FlutterLogo(),
|
17
|
+
),
|
18
|
+
);
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
4
22
|
class LogoApp extends StatefulWidget {
|
5
23
|
const LogoApp({Key? key}) : super(key: key);
|
6
24
|
@override
|
7
25
|
_LogoAppState createState() => _LogoAppState();
|
8
26
|
}
|
@@ -16,32 +34,18 @@
|
|
16
34
|
@override
|
17
35
|
void initState() {
|
18
36
|
super.initState();
|
19
37
|
controller =
|
20
38
|
AnimationController(duration: const Duration(seconds: 2), vsync: this);
|
21
|
-
animation = Tween<double>(begin: 0, end: 300).animate(controller)
|
22
|
-
..addListener(() {
|
23
|
-
setState(() {
|
24
|
-
// The state that has changed here is the animation object’s value.
|
25
|
-
});
|
26
|
-
});
|
39
|
+
animation = Tween<double>(begin: 0, end: 300).animate(controller);
|
27
40
|
controller.forward();
|
28
41
|
}
|
29
42
|
@override
|
30
|
-
Widget build(BuildContext context)
|
31
|
-
return Center(
|
32
|
-
child: Container(
|
33
|
-
margin: const EdgeInsets.symmetric(vertical: 10),
|
34
|
-
height: animation.value,
|
35
|
-
width: animation.value,
|
36
|
-
child: const FlutterLogo(),
|
37
|
-
),
|
38
|
-
);
|
39
|
-
}
|
43
|
+
Widget build(BuildContext context) => AnimatedLogo(animation: animation);
|
40
44
|
@override
|
41
45
|
void dispose() {
|
42
46
|
controller.dispose();
|
43
47
|
super.dispose();
|
44
48
|
}
|
App source: animate2
Monitoring the progress of the animation
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:
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((state) => print('$state'));
controller.forward();
}
// ...
}
Running this code produces this output:
AnimationStatus.forward
AnimationStatus.completed
Next, use addStatusListener()
to reverse the animation
at the beginning or the end. This creates a “breathing” effect:
@@ -36,7 +36,15 @@
|
|
36
36
|
void initState() {
|
37
37
|
super.initState();
|
38
38
|
controller =
|
39
39
|
AnimationController(duration: const Duration(seconds: 2), vsync: this);
|
40
|
-
animation = Tween<double>(begin: 0, end: 300).animate(controller)
|
40
|
+
animation = Tween<double>(begin: 0, end: 300).animate(controller)
|
41
|
+
..addStatusListener((status) {
|
42
|
+
if (status == AnimationStatus.completed) {
|
43
|
+
controller.reverse();
|
44
|
+
} else if (status == AnimationStatus.dismissed) {
|
45
|
+
controller.forward();
|
46
|
+
}
|
47
|
+
})
|
48
|
+
..addStatusListener((state) => print('$state'));
|
41
49
|
controller.forward();
|
42
50
|
}
|
App source: animate3
Refactoring with AnimatedBuilder
One problem with the code in the animate3 example, is that changing the animation required changing the widget that renders the logo. A better solution is to separate responsibilities into different classes:
- Render the logo
- Define the
Animation
object - Render the transition
You can accomplish this separation with the help of the
AnimatedBuilder
class. An AnimatedBuilder
is a
separate class in the render tree. Like AnimatedWidget
,
AnimatedBuilder
automatically listens to notifications
from the Animation
object, and marks the widget tree
dirty as necessary, so you don’t need to call addListener()
.
The widget tree for the animate4 example looks like this:
<img src=’/assets/images/docs/’ui/AnimatedBuilder-WidgetTree.png’’ alt=”AnimatedBuilder widget tree” class=”d-block mx-auto” width=”160px”>
Starting from the bottom of the widget tree, the code for rendering the logo is straightforward:
class LogoWidget extends StatelessWidget { const LogoWidget({Key? key}) : super(key: key); // Leave out the height and width so it fills the animating parent @override Widget build(BuildContext context) { return Container( margin: const EdgeInsets.symmetric(vertical: 10), child: const FlutterLogo(), ); } }
The middle three blocks in the diagram are all created in the
build()
method in GrowTransition
, shown below.
The GrowTransition
widget itself is stateless and holds
the set of final variables necessary to define the transition animation.
The build() function creates and returns the AnimatedBuilder
,
which takes the (Anonymous
builder) method and the
LogoWidget
object as parameters. The work of rendering the
transition actually happens in the (Anonymous
builder)
method, which creates a Container
of the appropriate size
to force the LogoWidget
to shrink to fit.
One tricky point in the code below is that the child looks
like it’s specified twice. What’s happening is that the
outer reference of child is passed to AnimatedBuilder
,
which passes it to the anonymous closure, which then uses
that object as its child. The net result is that the
AnimatedBuilder
is inserted in between the two widgets
in the render tree.
class GrowTransition extends StatelessWidget { const GrowTransition({required this.child, required this.animation, Key? key}) : super(key: key); final Widget child; final Animation<double> animation; @override Widget build(BuildContext context) { return Center( child: AnimatedBuilder( animation: animation, builder: (context, child) { return SizedBox( height: animation.value, width: animation.value, child: child, ); }, child: child, ), ); } }
Finally, the code to initialize the animation looks very
similar to the animate2 example. The initState()
method creates an AnimationController
and a Tween
,
then binds them with animate()
. The magic happens in
the build()
method, which returns a GrowTransition
object with a LogoWidget
as a child, and an animation object to
drive the transition. These are the three elements listed
in the bullet points above.
@@ -1,28 +1,48 @@
|
|
1
1
|
import 'package:flutter/animation.dart';
|
2
2
|
import 'package:flutter/material.dart';
|
3
3
|
void main() => runApp(const LogoApp());
|
4
|
-
class
|
5
|
-
const
|
6
|
-
|
4
|
+
class LogoWidget extends StatelessWidget {
|
5
|
+
const LogoWidget({Key? key}) : super(key: key);
|
6
|
+
|
7
|
+
// Leave out the height and width so it fills the animating parent
|
8
|
+
@override
|
9
|
+
Widget build(BuildContext context) {
|
10
|
+
return Container(
|
11
|
+
margin: const EdgeInsets.symmetric(vertical: 10),
|
12
|
+
child: const FlutterLogo(),
|
13
|
+
);
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
class GrowTransition extends StatelessWidget {
|
18
|
+
const GrowTransition({required this.child, required this.animation, Key? key})
|
19
|
+
: super(key: key);
|
20
|
+
|
21
|
+
final Widget child;
|
22
|
+
final Animation<double> animation;
|
7
23
|
@override
|
8
24
|
Widget build(BuildContext context) {
|
9
|
-
final animation = listenable as Animation<double>;
|
10
25
|
return Center(
|
11
|
-
child:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
26
|
+
child: AnimatedBuilder(
|
27
|
+
animation: animation,
|
28
|
+
builder: (context, child) {
|
29
|
+
return SizedBox(
|
30
|
+
height: animation.value,
|
31
|
+
width: animation.value,
|
32
|
+
child: child,
|
33
|
+
);
|
34
|
+
},
|
35
|
+
child: child,
|
16
36
|
),
|
17
37
|
);
|
18
38
|
}
|
19
39
|
}
|
20
40
|
class LogoApp extends StatefulWidget {
|
21
41
|
const LogoApp({Key? key}) : super(key: key);
|
22
42
|
@override
|
23
43
|
_LogoAppState createState() => _LogoAppState();
|
@@ -35,18 +55,23 @@
|
|
35
55
|
@override
|
36
56
|
void initState() {
|
37
57
|
super.initState();
|
38
58
|
controller =
|
39
59
|
AnimationController(duration: const Duration(seconds: 2), vsync: this);
|
40
60
|
animation = Tween<double>(begin: 0, end: 300).animate(controller);
|
41
61
|
controller.forward();
|
42
62
|
}
|
43
63
|
@override
|
44
|
-
Widget build(BuildContext context)
|
64
|
+
Widget build(BuildContext context) {
|
65
|
+
return GrowTransition(
|
66
|
+
child: const LogoWidget(),
|
67
|
+
animation: animation,
|
68
|
+
);
|
69
|
+
}
|
45
70
|
@override
|
46
71
|
void dispose() {
|
47
72
|
controller.dispose();
|
48
73
|
super.dispose();
|
49
74
|
}
|
50
75
|
}
|
App source: animate4
Simultaneous animations
In this section, you’ll build on the example from
monitoring the progress of the animation
(animate3), which used AnimatedWidget
to animate in and out continuously. Consider the case
where you want to animate in and out while the
opacity animates from transparent to opaque.
Each tween manages an aspect of the animation. For example:
controller = AnimationController(duration: const Duration(seconds: 2), vsync: this); sizeAnimation = Tween<double>(begin: 0, end: 300).animate(controller); opacityAnimation = Tween<double>(begin: 0.1, end: 1).animate(controller);
You can get the size with sizeAnimation.value
and the opacity
with opacityAnimation.value
, but the constructor for AnimatedWidget
only takes a single Animation
object. To solve this problem,
the example creates its own Tween
objects and explicitly calculates the
values.
Change AnimatedLogo
to encapsulate its own Tween
objects,
and its build()
method calls Tween.evaluate()
on the parent’s animation object to calculate
the required size and opacity values.
The following code shows the changes with highlights:
class AnimatedLogo extends AnimatedWidget { const AnimatedLogo({Key? key, required Animation<double> animation}) : super(key: key, listenable: animation); // Make the Tweens static because they don't change. static final _opacityTween = Tween<double>(begin: 0.1, end: 1); static final _sizeTween = Tween<double>(begin: 0, end: 300); @override Widget build(BuildContext context) { final animation = listenable as Animation<double>; return Center( child: Opacity( opacity: _opacityTween.evaluate(animation), child: Container( margin: const EdgeInsets.symmetric(vertical: 10), height: _sizeTween.evaluate(animation), width: _sizeTween.evaluate(animation), child: const FlutterLogo(), ), ), ); } } class LogoApp extends StatefulWidget { const LogoApp({Key? key}) : super(key: key); @override _LogoAppState createState() => _LogoAppState(); } 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 = CurvedAnimation(parent: controller, curve: Curves.easeIn) ..addStatusListener((status) { if (status == AnimationStatus.completed) { controller.reverse(); } else if (status == AnimationStatus.dismissed) { controller.forward(); } }); controller.forward(); } @override Widget build(BuildContext context) => AnimatedLogo(animation: animation); @override void dispose() { controller.dispose(); super.dispose(); } }
App source: animate5
Next steps
This tutorial gives you a foundation for creating animations in
Flutter using Tweens
, but there are many other classes to explore.
You might investigate the specialized Tween
classes,
animations specific to Material Design,
ReverseAnimation
,
shared element transitions (also known as Hero animations),
physics simulations and fling()
methods.
See the animations landing page
for the latest available documents and examples.