Animations Made Easy
Animations make an app feel alive. Flutter has two families: implicit (easy, automatic) and explicit (full control). Start with implicit ones. You only change a value, and Flutter animates from the old value to the new value for you.
AnimatedContainer
dart
class BoxDemo extends StatefulWidget {
const BoxDemo({super.key});
@override
State<BoxDemo> createState() => _BoxDemoState();
}
class _BoxDemoState extends State<BoxDemo> {
bool big = false;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
width: big ? 220 : 100,
height: big ? 220 : 100,
decoration: BoxDecoration(
color: big ? Colors.orange : Colors.blue,
borderRadius: BorderRadius.circular(big ? 60 : 12),
),
),
const SizedBox(height: 24),
FilledButton(
onPressed: () => setState(() => big = !big),
child: const Text('Animate'),
),
],
);
}
}
When you press the button, setState changes big. The container animates its size, color and corners smoothly over half a second.
Other Ready-Made Implicit Widgets
| Widget | Animates |
AnimatedOpacity | Fade in and out |
AnimatedAlign / AnimatedPositioned | Position changes |
AnimatedPadding | Padding changes |
AnimatedScale / AnimatedRotation / AnimatedSlide | Size, turn and move |
AnimatedDefaultTextStyle | Text style changes |
AnimatedSwitcher | Switching between two different child widgets |
AnimatedCrossFade | Cross-fade between two widgets |
dart
AnimatedOpacity(
opacity: visible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 300),
child: const Text('Hello'),
)
AnimatedSwitcher for Changing Content
dart
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) =>
ScaleTransition(scale: animation, child: child),
child: Text(
'$count',
key: ValueKey<int>(count), // the key tells Flutter the child changed
style: const TextStyle(fontSize: 48),
),
)
Why a key? AnimatedSwitcher animates only when the child's key or type changes. Without a different key, the new Text looks like the same widget and nothing animates.
TweenAnimationBuilder: Custom Values
dart
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: 1),
duration: const Duration(seconds: 2),
builder: (context, value, child) {
return Opacity(
opacity: value,
child: Transform.scale(scale: 0.5 + value * 0.5, child: child),
);
},
child: const FlutterLogo(size: 100),
)
Curves
| Curve | Feel |
Curves.linear | Constant speed, robotic |
Curves.easeInOut | Starts and ends slowly, natural |
Curves.bounceOut | Bounces at the end |
Curves.elasticOut | Springy |
Curves.fastOutSlowIn | Material standard |
Design tip: Keep animations short (150 to 400 milliseconds) and purposeful. Too much motion annoys users.
Conclusion
Implicit animations give great results with almost no code. Change a value, add a duration and a curve, and Flutter does the rest.
Quick Summary: Use AnimatedContainer and its relatives, set duration and curve, and give AnimatedSwitcher a key.