Series 6 — Part 1 of 1
Flutter Animations: From Implicit to Explicit, and When to Use Each
A standalone guide on mastering Flutter's animation primitives
Animations had been on my backlog for weeks. Everything I built was functional but static. My mentor suggested I dedicate time specifically to the Flutter animation system — not just using packages, but understanding how the underlying primitives work.
I spent two days reading the Flutter animation docs and implementing a series of examples that moved from the simplest implicit animations up to AnimationController with custom curves and sequences.
Implicit animations: the easy starting point
Flutter's implicit animation widgets handle the AnimationController lifecycle for you. AnimatedContainer is the classic example — change any property (color, size, padding) and Flutter will tween between the old and new values over a specified duration:
AnimatedContainer(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
width: _expanded ? 200 : 100,
color: _expanded ? Colors.blue : Colors.grey,
)
Other useful implicit widgets:
-
AnimatedOpacity(fade in/out) AnimatedPaddingAnimatedDefaultTextStyle-
AnimatedPositionedinside aStack
These cover most UI transitions without any manual controller management.
Explicit animations with AnimationController
When you need full control — custom sequences, interruption handling, or value-driven animations — you move to explicit animations with AnimationController:
class _MyWidgetState extends State<MyWidget> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _opacity;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 500),
vsync: this,
);
_opacity = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeIn),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
The vsync: this mixin (SingleTickerProviderStateMixin) ties the animation to the widget's frame callback, preventing off-screen animations from consuming resources.
Fixing bugs in the implementation
-
Missed First Frame: I hit a bug where the animation was completing but the widget was not rebuilding. The cause: I was calling
setState()inside the animation listener, but the listener was added after_controller.forward()— so the first frame was missed. The fix was to add the listener before callingforward(). -
Controller in ViewModel anti-pattern: I also saw a common mistake when integrating animations into an MVVM app: putting
AnimationControllerinside a ViewModel. This does not work becauseAnimationControllerrequires aTickerProvider(a vsync), which is a widget concern.AnimationControllerbelongs in aStateclass, not a ViewModel.
What I learned
-
Start simple: Start with implicit animations (
AnimatedContainer,AnimatedOpacity) — they cover most use cases. -
Control with explicit: Use explicit
AnimationControllerwhen you need sequencing, interruption, or programmatic control. -
Lifecycle cleanup: Always dispose
AnimationControllerindispose()— skipping this leaks memory. -
MVVM boundary:
AnimationControllerbelongs inState, notViewModel— it requires aTickerProvider. -
Listener order: Add animation listeners before calling
forward()orreverse()to catch the first frame.
Top comments (0)