Implicit animations

Welcome to the implicit animations codelab, where you learn how to use Flutter widgets that make it easy to create animations for a specific set of properties.

To get the most out of this codelab, you should have basic knowledge about:

This codelab covers the following material:

  • Using AnimatedOpacity to create a fade-in effect.
  • Using AnimatedContainer to animate transitions in size, color, and margin.
  • Overview of implicit animations and techniques for using them.

Estimated time to complete this codelab: 15-30 minutes.

What are implicit animations?

With Flutter’s animation library, you can add motion and create visual effects for the widgets in your UI. One part of the library is an assortment of widgets that manage animations for you. These widgets are collectively referred to as implicit animations, or implicitly animated widgets, deriving their name from the ImplicitlyAnimatedWidget class that they implement. With implicit animations, you can animate a widget property by setting a target value; whenever that target value changes, the widget animates the property from the old value to the new one. In this way, implicit animations trade control for convenience—they manage animation effects so that you don’t have to.

Example: Fade-in text effect

The following example shows how to add a fade-in effect to existing UI using an implicitly animated widget called AnimatedOpacity. The example begins with no animation code—it consists of a Material App home screen containing:

  • A photograph of an owl.
  • One Show details button that does nothing when clicked.
  • Description text of the owl in the photograph.

Fade-in (starter code)

Click the Run button to run the example:

Animate opacity with AnimatedOpacity widget

This section contains a list of steps you can use to add an implicit animation to the fade-in starter code. After the steps, you can also run the fade-in complete code with the the changes already made. The steps outline how to use the AnimatedOpacity widget to add the following animation feature:

  • The owl’s description text remains hidden until the user clicks the Show details button.
  • When the user clicks the Show details button, the owl’s description text fades in.

1. Pick a widget property to animate

To create a fade-in effect, you can animate the opacity property using the AnimatedOpacity widget. Change the Container widget to an AnimatedOpacity widget:

{opacity1 → opacity2}/lib/main.dart
@@ -23,7 +23,7 @@
23
23
  style: TextStyle(color: Colors.blueAccent),
24
24
  ),
25
25
  onPressed: () => null),
26
- Container(
26
+ AnimatedOpacity(
27
27
  child: Column(
28
28
  children: <Widget>[
29
29
  Text('Type: Owl'),

2. Initialize a state variable for the animated property

To hide the text before the user clicks Show details, set the starting value for opacity to zero:

{opacity2 → opacity3}/lib/main.dart
@@ -13,6 +13,8 @@
13
13
  }
14
14
  class _FadeInDemoState extends State<FadeInDemo> {
15
+ double opacity = 0.0;
16
+
15
17
  @override
16
18
  Widget build(BuildContext context) {
17
19
  return Column(children: <Widget>[
@@ -24,6 +26,7 @@
24
26
  ),
25
27
  onPressed: () => null),
26
28
  AnimatedOpacity(
29
+ opacity: opacity,
27
30
  child: Column(
28
31
  children: <Widget>[
29
32
  Text('Type: Owl'),

3. Set up a trigger for the animation, and choose an end value

Configure the animation to trigger when the user clicks the Show details button. To do this, change opacity state using the onPressed() handler for MaterialButton. To make the FadeInDemo widget become fully visible when the user clicks the Show details button, use the onPressed() handler to set opacity to 1:

{opacity4 → opacity5}/lib/main.dart
@@ -20,11 +20,14 @@
20
20
  return Column(children: <Widget>[
21
21
  Image.network(owl_url),
22
22
  MaterialButton(
23
- child: Text(
24
- 'Show Details',
25
- style: TextStyle(color: Colors.blueAccent),
26
- ),
27
- onPressed: () => null),
23
+ child: Text(
24
+ 'Show Details',
25
+ style: TextStyle(color: Colors.blueAccent),
26
+ ),
27
+ onPressed: () => setState(() {
28
+ opacity = 1;
29
+ }),
30
+ ),
28
31
  AnimatedOpacity(
29
32
  duration: Duration(seconds: 2),
30
33
  opacity: opacity,

4. Set the duration of the animation

In addition to an opacity parameter, AnimatedOpacity requires a duration to use for its animation. For this example, you can start with 2 seconds:

{opacity3 → opacity4}/lib/main.dart
@@ -26,6 +26,7 @@
26
26
  ),
27
27
  onPressed: () => null),
28
28
  AnimatedOpacity(
29
+ duration: Duration(seconds: 2),
29
30
  opacity: opacity,
30
31
  child: Column(
31
32
  children: <Widget>[

Fade-in (complete)

Here’s the example with the completed changes you’ve made—run this example and click the Show details button to trigger the animation.

Putting it all together

The Fade-in text effect example demonstrates the following features of AnimatedOpacity:

  • AnimatedOpacity listens for state changes in its opacity property.
  • Whenever opacity changes, AnimatedOpacity automatically animates the widget’s transition to the new value for opacity.
  • AnimatedOpacity requires a duration parameter to define the time it takes to animate the transition between an old opacity value and a new one.

Example: Shape-shifting effect

The following example shows how to use the AnimatedContainer widget to animate multiple properties (margin, borderRadius, and color) with different types (double and Color). The example begins with no animation code—it starts with a Material App home screen that contains:

  • A Container with borderRadius, margin, and color properties that are different each time you run the example.
  • A Change button that does nothing when clicked.

Shape-shifting (starter code)

Click the Run button to run the example:

Animate color, borderRadius, and margin with AnimatedContainer

This section contains a list of steps you can use to add an implicit animation to the shape-shifting starter code. After the steps, you can also run the shape-shifting complete example with the changes already made.

In the shape-shifting starter code, each property in the Container widget (color, borderRadius, and margin) is assigned a value by an associated function (randomColor(), randomBorderRadius(), and randomMargin() respectively). By using an AnimatedContainer widget, you can refactor this code to do the following:

  • Generate new values for color, borderRadius, and margin whenever the user clicks the Change button.
  • Animate the transition to the new values for color, borderRadius, and margin whenever they are set.

1. Add an implicit animation

Change the Container widget to an AnimatedContainer widget:

{container1 → container2}/lib/main.dart
@@ -45,7 +45,7 @@
45
45
  SizedBox(
46
46
  width: 128,
47
47
  height: 128,
48
- child: Container(
48
+ child: AnimatedContainer(
49
49
  margin: EdgeInsets.all(margin),
50
50
  decoration: BoxDecoration(
51
51
  color: color,

2. Set starting values for animated properties

AnimatedContainer automatically animates between old and new values of its properties when they change. Create a change() method that defines the behavior triggered when the user clicks the Change button. The change() method can use setState() to set new values for the color, borderRadius, and margin state variables:

{container2 → container3}/lib/main.dart
@@ -36,6 +36,14 @@
36
36
  margin = randomMargin();
37
37
  }
38
+ void change() {
39
+ setState(() {
40
+ color = randomColor();
41
+ borderRadius = randomBorderRadius();
42
+ margin = randomMargin();
43
+ });
44
+ }
45
+
38
46
  @override
39
47
  Widget build(BuildContext context) {
40
48
  return Scaffold(

3. Set up a trigger for the animation

To set the animation to trigger whenever the user presses the Change button, invoke the change() method in the onPressed() handler:

{container3 → container4}/lib/main.dart
@@ -67,7 +67,7 @@
67
67
  'change',
68
68
  style: TextStyle(color: Colors.white),
69
69
  ),
70
- onPressed: () => null,
70
+ onPressed: () => change(),
71
71
  ),
72
72
  ],
73
73
  ),

4. Set duration

Finally, set the duration of the animation that powers the transition between the old and new values:

{container4 → container5}/lib/main.dart
@@ -8,6 +8,8 @@
8
8
  import 'package:flutter_web_test/flutter_web_test.dart';
9
9
  import 'package:flutter_web_ui/ui.dart' as ui;
10
+ const _duration = Duration(milliseconds: 400);
11
+
10
12
  double randomBorderRadius() {
11
13
  return Random().nextDouble() * 64;
12
14
  }
@@ -60,6 +62,7 @@
60
62
  borderRadius: BorderRadius.circular(borderRadius),
61
63
  ),
62
64
  ),
65
+ duration: _duration,
63
66
  ),
64
67
  MaterialButton(
65
68
  color: Theme.of(context).primaryColor,

Shape-shifting (complete)

Here’s the example with the completed changes you’ve made—run the code and click the Change button to trigger the animation. Notice that each time you click the Change button, the shape animates to its new values for margin, borderRadius, and color.

Using animation curves

The preceding examples show how implicit animations allow you to animate changes in values for specific widget properties, and how the duration parameter allows you to set the amount of time an animation takes to complete. Implicit animations also allow you to control changes to the rate of an animation within the duration. The parameter you use to define this change in rate is curve.

The preceding examples do not specify a curve, so the implicit animations apply a linear animation curve by default. Add a curve parameter to the shape-shifting complete and watch how the animation changes when you pass the easeInOutBack constant for curve:

{container5 → container6}/lib/main.dart
@@ -63,6 +63,7 @@
63
63
  ),
64
64
  ),
65
65
  duration: _duration,
66
+ curve: Curves.easeInOutBack,
66
67
  ),
67
68
  MaterialButton(
68
69
  color: Theme.of(context).primaryColor,

Now that you have passed easeInOutBack as the value for curve to AnimatedContainer, notice that the rates of change for margin, borderRadius, and color follow the curve defined by the easeInOutBack curve:

Putting it all together

The shape-shifting complete example animates transitions between values for margin, borderRadius, and color properties. Note that AnimatedContainer animates changes to any of its properties, including those you didn’t use such as padding, transform, and even child and alignment! The [shape-shifting complete example builds upon fade-in complete by showing additional capabilities of implicit animations:

  • Some implicit animations (for example, AnimatedOpacity) only animate a single property, while others (like AnimatedContainer) can animate many properties.
  • Implicit animations automatically animate between the old and new values of properties when they change using the provided curve and duration.
  • If you do not specify a curve, implicit animations default to a linear curve.

What’s Next?

Congratulations, you’ve finished the codelab! If you’d like to learn more, here are some suggestions for where to go next: