Sub topic 7
When implicit animations are not enough
This chapter assumes you know Dart classes, mixins, closures, Flutter’s build method, and StatefulWidget with setState. You should also know basic layout widgets such as Container, Row, and Column.
Implicit animations are useful when a property should move from its old value to a new value after a rebuild. You change width, color, or opacity, and Flutter handles the frames between those values.
They become limiting when your code must control the animation itself.
Recognizing the limits
An implicit widget usually gives you a small control surface:
- A
durationcontrols how long the change takes. - A
curvecontrols the speed pattern. - An
onEndcallback can tell you that the animation finished. - A new property value starts a new transition.
This is enough for independent property changes. It is less suitable when you need to pause, reverse, repeat, seek, or coordinate several effects.
| Requirement | Implicit animation | Explicit animation |
|---|---|---|
| Animate a changed property | Good fit | Works, but more code |
| Repeat continuously | Usually awkward | Built in with `repeat` |
| Reverse on demand | Limited | `reverse()` |
| Pause or seek | No direct progress control | Controller value and methods |
| Coordinate several effects | Possible with callbacks | One controller can drive all effects |
| React to animation status | Limited | Status listeners |
For example, a menu that opens and closes can use AnimatedContainer. A pulsing notification badge, however, needs a repeatable animation. A wizard entrance may need one effect to fade in while another slides later. These cases need an explicit animation.
Moving to an explicit controller
An explicit animation uses an AnimationController. The controller produces a value that normally moves from 0.0 to 1.0. Your widget maps that value to a visual property.
The state object must provide a ticker. A ticker produces animation updates while the widget is visible. The SingleTickerProviderStateMixin supplies one through vsync: this.
This example opens and closes a panel. The controller gives the code direct control over playback.
import 'package:flutter/material.dart';
class ExpandPanel extends StatefulWidget {
const ExpandPanel({super.key});
@override
State<ExpandPanel> createState() => _ExpandPanelState();
}
class _ExpandPanelState extends State<ExpandPanel>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 300),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _toggle() {
if (_controller.isCompleted) {
_controller.reverse();
} else {
_controller.forward();
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: _toggle,
child: const Text('Toggle'),
),
SizeTransition(
sizeFactor: _controller,
child: const Padding(
padding: EdgeInsets.all(16),
child: Text('Details are visible here.'),
),
),
],
);
}
}The controller is created in initState and disposed in dispose. Forgetting disposal can leave animation resources active after the widget leaves the tree.
Exercise
Why is an explicit controller a better choice for a pulsing badge than AnimatedContainer?
Show answer
The controller can repeat and reverse the animation directly, while AnimatedContainer mainly reacts to new property values.