Original address: medium.com/flutter/how…

The author is medium.com/@fitzface

To view this article in video form, check out our version hereYouTube video.

This article was originally written and posted under Emily Fortuna’s name.

So, you decide to add an animation to your Flutter app — how exciting! There are many different animated widgets, so trying to figure out which one is best for you can be overwhelming. The problem is, there are many different animated widgets, so trying to figure out which one is the best fit can be overwhelming. Fortunately, this article can help you!

I’m going to go through a series of questions that you can ask yourself about the animation you have in mind to help you determine how you should create it. It is also important to keep in mind that the animation widgets provided in the Flutter core library are quite low-level. This means that if you have a complex animation, I recommend you check out some of the libraries on pub.dev that offer higher levels of animation dependency.

Take a look at the decision tree below, which I’ll explain in this article:

In general, there are two main types of animation that you might want to include in your Flutter application: drawing based animation and code based animation.

By contrast, drawing based animation looks as if someone had drawn it. They are often stand-alone sprites, like game characters, or involve transformations expressed purely in code, which can be challenging.

So, the first question to ask yourself is: “Is my animation more like a drawing, or does it look like something that can be built with a Flutter Widget unit?” If your animation is more like a drawing, or if you work with a design team who will provide vector or raster image resources, then I recommend you use a third party tool such as Rive or Lottie to build your animation graphically and export it to Flutter. There are several software dependency libraries that can help you add these assets to your Flutter application.

Otherwise, if your animation involves Tween widgets —- such as changing colors, shapes or positions —- you will have to write some Flutter code!

Explicit or implicit?

There are two types of code-based Flutter animation: implicit and explicit. The next step is to determine what type of animation you need.

Implicit animation relies on simply setting a new value for some widget properties, while Flutter is responsible for animating them from the current value to the new value. These widgets are easy to use and very powerful. All of the animation you see above is done with the implicit animation widget. Implicit animation is a great place to start when you want to animate something.

Explicit animations require an AnimationController. They are called “explicit” because they only start animating when explicitly requested. You can use explicit animation to do all the things you can do with implicit animation, and more. Annoyingly, you have to manually manage the life cycle of the AnimationController because it is not a widget, which means putting it inside a stateful widget. For this reason, your code is usually simpler if you can use implicit animation widgets.

There are three questions to ask yourself to determine what type of widget you need. Will my animation repeat forever? And when I say forever, I mean when it’s on a certain screen, or under certain conditions, like when music is playing, it repeats forever.

The second question to ask yourself is whether the values in your animation are discontinuous. An example of what I mean by discontinuous animation is this growing circle animation. The circle keeps getting smaller and bigger and smaller and bigger. It never gets smaller or bigger and then shrinks back again. In this case, the size of the circle is discontinuous.

The final question to ask yourself is, do multiple widgets animate together harmoniously? Such as:

If you answered “yes” to any of these three questions, you need to use explicit widgets. Otherwise, you can use implicit widgets! Once you have decided whether you want an implicit or explicit widget, the last question will lead you to the specific widget you need.

Which widget?

Ask yourself, is there a built-in widget that fits my needs? If you’re looking for a built-in implicit animation widget, look for one called AnimatedFoo, where “Foo “is the property you want to animate, such as AnimatedFoo. Also, look at AnimatedContainer, because it’s a very powerful and versatile gadget for many different implicit animations.

If you can’t find the built-in implicit animation you need, you can use TweenAnimationBuilder to create a custom implicit animation. Instead, if you’re looking for an explicit widget built in, they’re usually called FooTransition, where “Foo “is the property you’re animating, like SlideTransition.

If you can’t find the relevant built-in explicit animation, there’s one last question you need to ask yourself. Do I want my animation to be a standalone widget, or part of another surrounding widget? The answer to this question depends largely on your taste. If you want a separate custom explicit animation, you should extend the AnimatedWidget. Otherwise, you can use AnimatedBuilder.

One final option to consider if you see performance issues is to animate with CustomPainter. You can use it like an AnimatedWidget, but CustomPainter paints directly on Canvas and there is no standard widget building paradigm. If used properly, you can create some neat, extremely custom effects or performance savings. However, if used incorrectly, your animations can cause more performance problems. So be careful, just like manual memory management, to make sure you know what you’re doing before spilling shared Pointers all over the floor.

conclusion

All in all, there are a series of high-level questions you can ask yourself that guide how to create animations. This series of questions creates a decision tree to determine which widget or dependency library is appropriate for your needs. If you fold the endpoints into a line, from left to right approximates the difficulty. Thank you for reading and please go out and create Great Flutter animations — either through third-party frameworks, or through explicit or implicit libraries of dependencies.


Editor’s note:

mermaid-live-editor

graph TD
  A{Animation} -->|Drawing-Based| b{Design Team}
  b -->|Yes| c[Lottie-Like]
  b -->|No| d[CustomPainter]
  A -->|Code-Based| C{Continuous}
  C -->|Yes| D{Widget}
  D -->|Yes| E[AnimatedFoo]
  D -->|No| F[TweenAnimationBuilder]
  C -->|No| G{FooTransition}
  G -->|Yes| H(explicitly)
  G -->|No| I{standalone}
  I -->|Yes| J[Sub AnimatedWidget]
  I -->|No| K[AnimatedBuilder]
Copy the code