Flutter Animations
Motion makes an interface feel alive and helps users understand what changed. Flutter has two families of animations:
- Implicit animations — widgets like
AnimatedContainerthat animate automatically whenever one of their properties changes. Easy and covers most needs. - Explicit animations — you drive an
AnimationControlleryourself. More code, but full control: looping, reversing, sequencing.
Start implicit; go explicit only when you need control.
AnimatedContainer
AnimatedContainer has the same properties as Container, plus a duration and an optional curve. Change a value in setState and it smoothly animates from the old value to the new one.
class GrowingBox extends StatefulWidget {
const GrowingBox({super.key});
@override
State<GrowingBox> createState() => _GrowingBoxState();
}
class _GrowingBoxState extends State<GrowingBox> {
bool _big = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() => _big = !_big),
child: AnimatedContainer(
duration: const Duration(milliseconds: 400),
curve: Curves.easeInOut,
width: _big ? 200 : 100,
height: _big ? 200 : 100,
decoration: BoxDecoration(
color: _big ? Colors.orange : Colors.blue,
borderRadius: BorderRadius.circular(_big ? 40 : 8),
),
),
);
}
}Other implicit widgets
| Widget | Animates |
|---|---|
AnimatedOpacity |
Fading in/out |
AnimatedAlign / AnimatedPositioned |
Position |
AnimatedPadding |
Padding |
AnimatedDefaultTextStyle |
Font size, colour, weight |
AnimatedSwitcher |
Cross-fade when its child is replaced |
AnimatedCrossFade |
Between two specific children |
TweenAnimationBuilder |
Any value you can tween — build your own |
class FadeMessage extends StatefulWidget {
const FadeMessage({super.key});
@override
State<FadeMessage> createState() => _FadeMessageState();
}
class _FadeMessageState extends State<FadeMessage> {
bool _visible = true;
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
AnimatedOpacity(
opacity: _visible ? 1 : 0,
duration: const Duration(milliseconds: 500),
child: const Text('Now you see me', style: TextStyle(fontSize: 24)),
),
TextButton(
onPressed: () => setState(() => _visible = !_visible),
child: const Text('Toggle'),
),
],
);
}
}Explicit animations with AnimationController
An AnimationController produces values from 0.0 to 1.0 over a duration, ticking once per frame. It needs a ticker, which you get by mixing SingleTickerProviderStateMixin into your State. Ready-made transition widgets such as RotationTransition, FadeTransition and ScaleTransition listen to it.
class Spinner extends StatefulWidget {
const Spinner({super.key});
@override
State<Spinner> createState() => _SpinnerState();
}
class _SpinnerState extends State<Spinner> with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 2),
)..repeat(); // loop forever
@override
void dispose() {
_controller.dispose(); // always dispose controllers
super.dispose();
}
@override
Widget build(BuildContext context) {
return RotationTransition(
turns: _controller,
child: const Icon(Icons.settings, size: 80, color: Colors.teal),
);
}
}To change the range or shape of the values, chain a Tween and a CurvedAnimation: Tween(begin: 0.5, end: 1.5).animate(CurvedAnimation(parent: _controller, curve: Curves.elasticOut)). Controller methods include forward(), reverse(), repeat(reverse: true) and stop().
Hero animations
A Hero widget animates a shared element between two screens. Wrap the image on both screens in a Hero with the same tag; when you navigate, Flutter flies it from one position to the other.
// List screen
Hero(tag: 'avatar-7', child: CircleAvatar(radius: 20, child: Text('A')))
// Detail screen
Hero(tag: 'avatar-7', child: CircleAvatar(radius: 80, child: Text('A')))Tip: Curves change the feel:
Curves.easeOutfor things entering,Curves.easeInfor things leaving,Curves.bounceOutorelasticOutfor playful effects. Keep most UI animations between 150 and 400 ms.
Test yourself
What triggers an AnimatedContainer to animate?
- Calling
forward() - Changing one of its properties and rebuilding
- Wrapping it in a Hero
- Setting
repeat: true
Which mixin provides the vsync needed by an AnimationController in a State?
- ChangeNotifier
- SingleTickerProviderStateMixin
- WidgetsBindingObserver
- AnimationMixin
Two Hero widgets on different screens animate between each other when they share the same…
- child
- tag
- size
- color