Sub topic 2
Implicit animations: let Flutter do the work
This chapter assumes you know Dart classes, mixins, generics, and closures. You should also know how StatelessWidget, StatefulWidget, setState, and basic layout widgets such as Container, Row, and Column work.
What an implicit animation does
An implicit animation starts when a widget receives a new property value. Flutter compares the old value with the new value, calculates intermediate values for each frame, and rebuilds the animated part of the widget tree. You describe the destination, while Flutter manages the controller and timing.
For example, changing a Container width from 100 to 200 normally changes it immediately. AnimatedContainer changes it gradually:
An implicit animation has three important parts:
- A state change produces a new target value.
- The widget interpolates between the old and new values.
durationandcurvedescribe how the change should look.
The first build has no earlier value to animate from. Animation begins on a later rebuild when an animated property changes.
A complete example with AnimatedContainer
Tap the box to switch between two states:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: AnimationDemo()));
}
class AnimationDemo extends StatefulWidget {
const AnimationDemo({super.key});
@override
State<AnimationDemo> createState() => _AnimationDemoState();
}
class _AnimationDemoState extends State<AnimationDemo> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () {
setState(() {
_expanded = !_expanded;
});
},
child: AnimatedContainer(
duration: const Duration(milliseconds: 400),
curve: Curves.easeInOut,
width: _expanded ? 220 : 120,
height: _expanded ? 220 : 120,
decoration: BoxDecoration(
color: _expanded ? Colors.teal : Colors.orange,
borderRadius: BorderRadius.circular(_expanded ? 32 : 8),
),
alignment: Alignment.center,
child: const Text('Tap me'),
),
),
),
);
}
}setState causes build to run again. The _expanded value selects a different width, height, color, and corner radius. AnimatedContainer sees those new values and animates each supported property over 400 milliseconds.
The widget does not animate every possible change. It animates properties that have compatible old and new values. A change to the widget's child replaces that child unless the child itself has an animation.
The implicit widget family
Flutter includes specialized widgets for common property changes:
| Widget | Commonly animates |
|---|---|
| `AnimatedOpacity` | Opacity |
| `AnimatedAlign` | Alignment |
| `AnimatedPadding` | Padding |
| `AnimatedPositioned` | Position inside a `Stack` |
| `AnimatedDefaultTextStyle` | Text style |
Use these when one widget clearly expresses the change. Use AnimatedContainer when several visual properties should move together.
Exercise
Exercise
Why does changing _expanded start an animation instead of changing the box immediately?
Show answer
setState rebuilds the widget with new target properties, and AnimatedContainer interpolates from its previous properties to those targets.