交织动画

What you’ll learn

  • A staggered animation consists of sequential or overlappinganimations.
  • To create a staggered animation, use multiple Animation objects.
  • One AnimationController controls all of the Animations.
  • Each Animation object specifies the animation during an Interval.
  • For each property being animated, create a Tween.

Terminology: If the concept of tweens or tweening is new to you, see the Animations in Flutter tutorial.

Staggered animations are a straightforward concept: visual changeshappen as a series of operations, rather than all at once.The animation might be purely sequential, with one change occuring afterthe next, or it might partially or completely overlap. It might alsohave gaps, where no changes occur.

This guide shows how to build a staggered animation in Flutter.

Examples

This guide explains the basic_staggered_animation example. You can also refer to a more complex example, staggered_pic_selection.

  • basic_staggered_animation
  • Shows a series of sequential and overlapping animations of a single widget.Tapping the screen begins an animation that changes opacity, size,shape, color, and padding.
  • staggered_pic_selection
  • Shows deleting an image from a list of images displayed in one of three sizes.This example uses two animationcontrollers:one for image selection/deselection, and one for image deletion.The selection/deselection animation is staggered. (To see this effect,you might need to increase the timeDilation value.)Select one of the largest images—it shrinks as it displays a checkmarkinside a blue circle. Next, select one of the smallest images—thelarge image expands as the checkmark disappears. Before the large imagehas finished expanding, the small image shrinks to display its checkmark.This staggered behavior is similar to what you might see in Google Photos.

The following video demonstrates the animation performed bybasic_staggered_animation:

In the video, you see the following animation of a single widget,which begins as a bordered blue square with slightly rounded corners.The square runs through changes in the following order:

  • Fades in
  • Widens
  • Becomes taller while moving upwards
  • Transforms into a bordered circle
  • Changes color to orangeAfter running forward, the animation runs in reverse.

New to Flutter?This page assumes you know how to create a layout using Flutter’swidgets. For more information, see Building Layouts inFlutter.

Basic structure of a staggered animation

What’s the point?

  • All of the animations are driven by the sameAnimationController.
  • Regardless of how long the animation lasts in real time,the controller’s values must be between 0.0 and 1.0, inclusive.
  • Each animation has anIntervalbetween 0.0 and 1.0, inclusive.
  • For each property that animates in an interval, create aTween.The Tween specifies the start and end values for that property.
  • The Tween produces anAnimationobject that is managed by the controller.

The following diagram shows the Intervals used in thebasic_staggered_animationexample. You might notice the following characteristics:

  • The opacity changes during the first 10% of the timeline.
  • A tiny gap occurs between the change in opacity, and the change in width.
  • Nothing animates during the last 25% of the timeline.
  • Increasing the padding makes the widget appear to rise upward.
  • Increasing the border radius to 0.5, transforms the square with roundedcorners into a circle.
  • The padding and border radius changes occur during the same exact interval,but they don’t have to.

Diagram showing the interval specified for each motion.

To set up the animation:

  • Create an AnimationController that manages all of the Animations.
  • Create a Tween for each property being animated.
    • The Tween defines a range of values.
    • The Tween’s animate method requires the parent controller, andproduces an Animation for that property.
  • Specify the interval on the Animation’s curve property.

When the controlling animation’s value changes, the new animation’svalue changes, triggering the UI to update.

The following code creates a tween for the width property.It builds aCurvedAnimation,specifying an eased curve.See Curvesfor other available pre-defined animation curves.

  1. width = Tween<double>(
  2. begin: 50.0,
  3. end: 150.0,
  4. ).animate(
  5. CurvedAnimation(
  6. parent: controller,
  7. curve: Interval(
  8. 0.125, 0.250,
  9. curve: Curves.ease,
  10. ),
  11. ),
  12. ),

The begin and end values don’t have to be doubles.The following code builds the tween for the borderRadius property(which controls the roundness of the square’s corners), usingBorderRadius.circular().

  1. borderRadius = BorderRadiusTween(
  2. begin: BorderRadius.circular(4.0),
  3. end: BorderRadius.circular(75.0),
  4. ).animate(
  5. CurvedAnimation(
  6. parent: controller,
  7. curve: Interval(
  8. 0.375, 0.500,
  9. curve: Curves.ease,
  10. ),
  11. ),
  12. ),

Complete staggered animation

Like all interactive widgets, the complete animation consistsof a widget pair: a stateless and a stateful widget.

The stateless widget specifies the Tweens,defines the Animation objects, and provides a build() functionresponsible for building the animating portion of the widget tree.

The stateful widget creates the controller, plays the animation,and builds the non-animating portion of the widget tree.The animation begins when a tap is detected anywhere in the screen.

Full code for basic_staggered_animation’s main.dart

Stateless widget: StaggerAnimation

In the stateless widget, StaggerAnimation, the build() function instantiates anAnimatedBuilder—ageneral purpose widget for building animations. The AnimatedBuilderbuilds a widget and configures it using the Tweens’ current values.The example creates a function named _buildAnimation() (which performsthe actual UI updates), and assigns it to its builder property.AnimatedBuilder listens to notifications from the animation controller,marking the widget tree dirty as values change.For each tick of the animation, the values are updated,resulting in a call to _buildAnimation().

  1. class StaggerAnimation extends StatelessWidget {
  2. StaggerAnimation({ Key key, this.controller }) :
  3.  
  4. // Each animation defined here transforms its value during the subset
  5. // of the controller's duration defined by the animation's interval.
  6. // For example the opacity animation transforms its value during
  7. // the first 10% of the controller's duration.
  8.  
  9. opacity = Tween<double>(
  10. begin: 0.0,
  11. end: 1.0,
  12. ).animate(
  13. CurvedAnimation(
  14. parent: controller,
  15. curve: Interval(
  16. 0.0, 0.100,
  17. curve: Curves.ease,
  18. ),
  19. ),
  20. ),
  21.  
  22. // ... Other tween definitions ...
  23.  
  24. super(key: key);
  25.  
  26. final Animation<double> controller;
  27. final Animation<double> opacity;
  28. final Animation<double> width;
  29. final Animation<double> height;
  30. final Animation<EdgeInsets> padding;
  31. final Animation<BorderRadius> borderRadius;
  32. final Animation<Color> color;
  33.  
  34. // This function is called each time the controller "ticks" a new frame.
  35. // When it runs, all of the animation's values will have been
  36. // updated to reflect the controller's current value.
  37. Widget _buildAnimation(BuildContext context, Widget child) {
  38. return Container(
  39. padding: padding.value,
  40. alignment: Alignment.bottomCenter,
  41. child: Opacity(
  42. opacity: opacity.value,
  43. child: Container(
  44. width: width.value,
  45. height: height.value,
  46. decoration: BoxDecoration(
  47. color: color.value,
  48. border: Border.all(
  49. color: Colors.indigo[300],
  50. width: 3.0,
  51. ),
  52. borderRadius: borderRadius.value,
  53. ),
  54. ),
  55. ),
  56. );
  57. }
  58.  
  59. @override
  60. Widget build(BuildContext context) {
  61. return AnimatedBuilder(
  62. builder: _buildAnimation,
  63. animation: controller,
  64. );
  65. }
  66. }

Stateful widget: StaggerDemo

The stateful widget, StaggerDemo, creates the AnimationController(the one who rules them all), specifying a 2000 ms duration. It playsthe animation, and builds the non-animating portion of the widget tree.The animation begins when a tap is detected in the screen.The animation runs forward, then backward.

  1. class StaggerDemo extends StatefulWidget {
  2. @override
  3. _StaggerDemoState createState() => _StaggerDemoState();
  4. }
  5.  
  6. class _StaggerDemoState extends State<StaggerDemo> with TickerProviderStateMixin {
  7. AnimationController _controller;
  8.  
  9. @override
  10. void initState() {
  11. super.initState();
  12.  
  13. _controller = AnimationController(
  14. duration: const Duration(milliseconds: 2000),
  15. vsync: this
  16. );
  17. }
  18.  
  19. // ...Boilerplate...
  20.  
  21. Future<void> _playAnimation() async {
  22. try {
  23. await _controller.forward().orCancel;
  24. await _controller.reverse().orCancel;
  25. } on TickerCanceled {
  26. // the animation got canceled, probably because we were disposed
  27. }
  28. }
  29.  
  30. @override
  31. Widget build(BuildContext context) {
  32. timeDilation = 10.0; // 1.0 is normal animation speed.
  33. return Scaffold(
  34. appBar: AppBar(
  35. title: const Text('Staggered Animation'),
  36. ),
  37. body: GestureDetector(
  38. behavior: HitTestBehavior.opaque,
  39. onTap: () {
  40. _playAnimation();
  41. },
  42. child: Center(
  43. child: Container(
  44. width: 300.0,
  45. height: 300.0,
  46. decoration: BoxDecoration(
  47. color: Colors.black.withOpacity(0.1),
  48. border: Border.all(
  49. color: Colors.black.withOpacity(0.5),
  50. ),
  51. ),
  52. child: StaggerAnimation(
  53. controller: _controller.view
  54. ),
  55. ),
  56. ),
  57. ),
  58. );
  59. }
  60. }

Resources

The following resources might help when writing animations: