The Containerclass provides a convenient way to create a widget with specific properties:width, height, background color, padding, borders, and more.

Simple animations often involve changing these properties over time.For example, you may want to animate the background color from grey to green toindicate that an item has been selected by the user.

To animate these properties, Flutter provides theAnimatedContainerwidget. Like the Container Widget, AnimatedContainer allows you to definethe width, height, background colors, and more. However, when theAnimatedContainer is rebuilt with new properties, it automaticallyanimates between the old and new values. In Flutter, these types ofanimations are known as “implicit animations.”

This recipe describes how to use an AnimatedContainer to animate the size,background color, and border radius when the user taps a button.

Directions

  • Create a StatefulWidget with default properties
  • Build an AnimatedContainer using the properties
  • Start the animation by rebuilding with new properties

1. Create a StatefulWidget with default properties

To start, createStatefulWidgetand State classes.Use the custom State class to define the properties you need to change overtime. In this example, that includes the width, height, color, and borderradius. In addition, you can also define the default value of each property.

These properties must belong to a custom State class so they can be updatedwhen the user taps a button.

  1. class AnimatedContainerApp extends StatefulWidget {
  2. @override
  3. _AnimatedContainerAppState createState() => _AnimatedContainerAppState();
  4. }
  5. class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
  6. // Define the various properties with default values. Update these properties
  7. // when the user taps a FloatingActionButton.
  8. double _width = 50;
  9. double _height = 50;
  10. Color _color = Colors.green;
  11. BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);
  12. @override
  13. Widget build(BuildContext context) {
  14. // Fill this out in the next steps
  15. }
  16. }

2. Build an AnimatedContainer using the properties

Next, you can build the AnimatedContainer using the properties defined in theprevious step. Furthermore, you must provide a duration that defines how longthe animation should run.

  1. AnimatedContainer(
  2. // Use the properties stored in the State class.
  3. width: _width,
  4. height: _height,
  5. decoration: BoxDecoration(
  6. color: _color,
  7. borderRadius: _borderRadius,
  8. ),
  9. // Define how long the animation should take.
  10. duration: Duration(seconds: 1),
  11. // Provide an optional curve to make the animation feel smoother.
  12. curve: Curves.fastOutSlowIn,
  13. );

3. Start the animation by rebuilding with new properties

Finally, start the animation by rebuilding the AnimatedContainer withnew properties. How to trigger a rebuild? When it comes to StatefulWidgets,setState is thesolution.

For this example, add a button to the app. When the user taps the button, updatethe properties with a new width, height, background color and border radius inside a call to setState.

In a real app, you most often transition between fixed values (for example, froma grey to a green background). For this app, generate new values each time theuser taps the button.

  1. FloatingActionButton(
  2. child: Icon(Icons.play_arrow),
  3. // When the user taps the button
  4. onPressed: () {
  5. // Use setState to rebuild the widget with new values.
  6. setState(() {
  7. // Create a random number generator.
  8. final random = Random();
  9. // Generate a random width and height.
  10. _width = random.nextInt(300).toDouble();
  11. _height = random.nextInt(300).toDouble();
  12. // Generate a random color.
  13. _color = Color.fromRGBO(
  14. random.nextInt(256),
  15. random.nextInt(256),
  16. random.nextInt(256),
  17. 1,
  18. );
  19. // Generate a random border radius.
  20. _borderRadius =
  21. BorderRadius.circular(random.nextInt(100).toDouble());
  22. });
  23. },
  24. );

Complete example

  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. void main() => runApp(AnimatedContainerApp());
  4. class AnimatedContainerApp extends StatefulWidget {
  5. @override
  6. _AnimatedContainerAppState createState() => _AnimatedContainerAppState();
  7. }
  8. class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
  9. // Define the various properties with default values. Update these properties
  10. // when the user taps a FloatingActionButton.
  11. double _width = 50;
  12. double _height = 50;
  13. Color _color = Colors.green;
  14. BorderRadiusGeometry _borderRadius = BorderRadius.circular(8);
  15. @override
  16. Widget build(BuildContext context) {
  17. return MaterialApp(
  18. home: Scaffold(
  19. appBar: AppBar(
  20. title: Text('AnimatedContainer Demo'),
  21. ),
  22. body: Center(
  23. child: AnimatedContainer(
  24. // Use the properties stored in the State class.
  25. width: _width,
  26. height: _height,
  27. decoration: BoxDecoration(
  28. color: _color,
  29. borderRadius: _borderRadius,
  30. ),
  31. // Define how long the animation should take.
  32. duration: Duration(seconds: 1),
  33. // Provide an optional curve to make the animation feel smoother.
  34. curve: Curves.fastOutSlowIn,
  35. ),
  36. ),
  37. floatingActionButton: FloatingActionButton(
  38. child: Icon(Icons.play_arrow),
  39. // When the user taps the button
  40. onPressed: () {
  41. // Use setState to rebuild the widget with new values.
  42. setState(() {
  43. // Create a random number generator.
  44. final random = Random();
  45. // Generate a random width and height.
  46. _width = random.nextInt(300).toDouble();
  47. _height = random.nextInt(300).toDouble();
  48. // Generate a random color.
  49. _color = Color.fromRGBO(
  50. random.nextInt(256),
  51. random.nextInt(256),
  52. random.nextInt(256),
  53. 1,
  54. );
  55. // Generate a random border radius.
  56. _borderRadius =
  57. BorderRadius.circular(random.nextInt(100).toDouble());
  58. });
  59. },
  60. ),
  61. ),
  62. );
  63. }
  64. }

AnimatedContainer demo showing a box growing and shrinking in size while changing color and border radius