Why Animations in Flutter?
Animations bring your app to life. They provide visual feedback, guide user attention, and create a polished user experience. Flutter offers two main approaches to animations: implicit animations (easy, for simple property changes) and explicit animations (full control over animation curves, timing, and composition). This guide covers both, giving you the tools to create everything from subtle fades to complex choreographed sequences.
Implicit Animations (Simple, Automatic)
Implicit animations are widgets that automatically animate when you change their properties. They are perfect for simple transitions like fading, scaling, rotating, or changing color/size. Common implicit widgets: AnimatedContainer, AnimatedOpacity, AnimatedPadding, AnimatedAlign, AnimatedPositioned, AnimatedDefaultTextStyle.
In this example, tapping toggles a boolean, and the AnimatedContainer animates between sizes and colors over 300ms.
Explicit Animations (Full Control)
Explicit animations give you complete control over the animation lifecycle. You use an AnimationController to drive the animation, Tween to define the range of values, and an AnimatedBuilder or AnimatedWidget to rebuild the widget with the current animation value. This pattern is ideal for complex, continuous, or looping animations.
AnimationController
The AnimationController produces a value from 0.0 to 1.0 over a specified duration. It can be started, stopped, repeated, or reversed. You must dispose it in dispose().
Tween
A Tween maps the controller's 0.0–1.0 range to a different range of values. For example, to animate between two colors, use ColorTween. To animate position, use Tween<Offset> or Tween<double> for scale, opacity, etc.
AnimatedBuilder – Efficient Rebuilding
AnimatedBuilder listens to an animation and rebuilds only the parts that depend on it. It's more efficient than a full widget rebuild. You provide a builder that returns the widget tree, and an optional child that won't rebuild (performance optimization).
AnimatedWidget – Reusable Animated Widgets
AnimatedWidget is a base class for creating widgets that listen to an animation and rebuild when it changes. It's a cleaner alternative to AnimatedBuilder when you have a single animation to listen to and want to encapsulate the logic.
Common Animation Patterns
- Fade transition: Use
AnimatedOpacityor aTween<double>withOpacitywidget.
- Fade transition: Use
- Size transition: Use
AnimatedContainerorTween<double>withTransform.scale.
- Size transition: Use
- Position transition: Use
AnimatedPositioned(inside aStack) orSlideTransition.
- Position transition: Use
- Rotation: Use
AnimatedBuilderwithTransform.rotateand aTween<double>.
- Rotation: Use
- Color transition: Use
AnimatedContainerorColorTweenwithAnimatedBuilder.
- Color transition: Use
Curves and Intervals
Curves control the rate of change of an animation. Flutter provides many built‑in curves: Curves.linear, Curves.easeIn, Curves.easeOut, Curves.bounceIn, etc. You can also create custom curves. Apply a curve by passing it to the AnimationController or by using CurvedAnimation.
Staggered Animations
Staggered animations sequence multiple animations using a single AnimationController. You can use Interval to delay or overlap parts of the animation. For example, you might fade in one element, then slide another, then rotate a third.
Key Takeaways
- Implicit animations (e.g.,
AnimatedContainer) are simple and automatic for property changes.
- Implicit animations (e.g.,
- Explicit animations use
AnimationController,Tween, andAnimatedBuilderfor full control.
- Explicit animations use
- Always dispose of
AnimationControllerindispose().
- Always dispose of
- Use
CurvedAnimationto apply easing curves.
- Use
- For multiple animations, use staggered intervals with a single controller.
AnimatedWidgetis great for reusable animated components.