Sub topic 18
Choosing the Right Approach
This chapter assumes you know Dart classes, mixins, generics, closures, Flutter widgets, build, setState, and basic layout widgets. You also know that a StatefulWidget can keep changing state.
Flutter offers two animation styles:
- Implicit animation: you change a widget property, and Flutter animates from the old value to the new value.
- Explicit animation: you create an
AnimationController, control its timeline, and connect it to widgets yourself.
Both styles produce frames by rebuilding or repainting with changing values. The difference is who controls the animation.
Use implicit animations for simple state changes
Choose an implicit widget when one state change should smoothly update one or more properties. AnimatedContainer, AnimatedOpacity, AnimatedAlign, and AnimatedPadding are good examples.
The code stays small because Flutter creates and manages the transition internally.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ColorDemo()));
class ColorDemo extends StatefulWidget {
const ColorDemo({super.key});
@override
State<ColorDemo> createState() => _ColorDemoState();
}
class _ColorDemoState extends State<ColorDemo> {
bool _selected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () => setState(() => _selected = !_selected),
child: AnimatedContainer(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
width: _selected ? 180 : 100,
height: _selected ? 180 : 100,
color: _selected ? Colors.teal : Colors.orange,
),
),
),
);
}
}This approach works well for a card that expands, a button that fades, or a panel that moves to a new alignment. TweenAnimationBuilder adds custom value types while keeping the same simple model.
Use explicit animations for timelines and control
Choose an explicit animation when you need to:
- Start, pause, reverse, or repeat at a precise time.
- Coordinate several effects.
- React to animation status changes.
- Use one timeline with different sections.
- Scrub or seek through progress.
- Share progress with several animated widgets.
Explicit animations require more lifecycle code. A state object usually owns an AnimationController, receives a ticker through a provider mixin, and disposes of the controller.
Compare the trade-offs
| Requirement | Best starting point | Reason |
|---|---|---|
| Animate a property after `setState` | Implicit | Minimal code and automatic timing |
| Repeat a pulse forever | Explicit | Playback needs a controller |
| Fade, then slide | Explicit | An `Interval` can divide one timeline |
| Animate a custom value once | `TweenAnimationBuilder` | Custom value without controller lifecycle |
| Pause or reverse from a button | Explicit | Direct playback control |
Common failure modes
An implicit animation can be the wrong choice when several effects must stay synchronized or when the animation must stop at an exact point. It also cannot directly expose a reusable controller to another part of the app.
An explicit animation can fail when its controller is not disposed, when a ticker continues after the widget leaves the tree, or when too many widgets rebuild on every tick. Keep static subtrees in the child parameter of AnimatedBuilder, and use transition widgets such as FadeTransition when possible.
Exercise
A panel must fade in, then slide upward, and a button must reverse the sequence. Which approach should you choose, and why?
Show answer
Choose an explicit AnimationController. Use intervals for the fade and slide, then call forward or reverse from the button.