Sub topic 1
How animation works in Flutter
Prerequisites
This chapter assumes you can write Dart classes, use generics, and pass closures. You should know how a StatefulWidget uses setState and build, and how to use layout widgets such as Container, Row, and Column.
How animation works in Flutter
Flutter draws the screen in frames, usually 60 or 120 per second. An animation is a value that changes over time. On each frame, Flutter calls build again, and the widget reads the current value and draws itself with it. The motion you see is a series of small changes, one per frame.
Implicit animations: let Flutter do the work
An implicit animation is a widget that animates its own properties. You change a property in setState, and the widget moves smoothly from the old value to the new one. You do not create a controller or manage frames yourself.
Shaping motion with duration and curves
duration sets how long the animation takes. A curve sets the speed profile over that time. Curves.linear moves at a constant speed, while Curves.easeInOut starts slowly, speeds up, and slows down at the end.
Your first implicit animation: AnimatedContainer
AnimatedContainer animates changes to size, color, and other decoration properties. Each tap switches between two sizes and colors.
import 'package:flutter/material.dart';
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,
color: _big ? Colors.teal : Colors.orange,
),
);
}
}AnimatedOpacity, AnimatedAlign and other implicit widgets
Flutter ships several implicit widgets. Each one animates one family of properties.
| Widget | Animated property |
|---|---|
| AnimatedContainer | Size, color, padding, and decoration |
| AnimatedOpacity | Transparency |
| AnimatedAlign | Alignment inside its parent |
| AnimatedPadding | Padding |
| AnimatedDefaultTextStyle | Text style |
AnimatedOpacity fades a child in and out:
AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 300),
child: const FlutterLogo(size: 64),
)Custom implicit animations with TweenAnimationBuilder
TweenAnimationBuilder animates any value that you define. It animates from the previous end value to the new end value, and its builder receives the current value on each frame.
import 'dart:math' as math;
import 'package:flutter/material.dart';
class RotatingIcon extends StatelessWidget {
const RotatingIcon({super.key, required this.turns});
final double turns;
@override
Widget build(BuildContext context) {
return TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 0, end: turns),
duration: const Duration(milliseconds: 500),
curve: Curves.easeOut,
builder: (context, value, child) {
return Transform.rotate(angle: value * 2 * math.pi, child: child);
},
child: const Icon(Icons.refresh, size: 48),
);
}
}Passing a larger turns value makes the icon rotate by the difference.
Exercise
In the AnimatedContainer example, what happens if you change duration to Duration.zero?
Show answer
The change happens instantly with no animation, because the widget jumps to the new values in a single frame.