Sub topic 3
Applying Curves with CurvedAnimation
This chapter assumes you know StatefulWidget and setState, can write a build method, and have seen AnimationController once before. It covers how curves shape motion, how to rebuild only what changes, the built-in transition widgets, playback control, disposal, staggered effects, and when to pick each approach.
Applying curves with CurvedAnimation
An AnimationController produces a value from 0.0 to 1.0 at a constant rate. A Curve remaps that value so motion speeds up or slows down. CurvedAnimation wraps a controller and applies a curve to it.
import 'package:flutter/material.dart';
class Pulse extends StatefulWidget {
const Pulse({super.key});
@override
State<Pulse> createState() => _PulseState();
}
class _PulseState extends State<Pulse> with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
late final Animation<double> _scale = Tween<double>(begin: 0.8, end: 1.2).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
@override
void initState() {
super.initState();
_controller.repeat(reverse: true);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(scale: _scale, child: const FlutterLogo(size: 80));
}
}Rebuilding efficiently with AnimatedBuilder
AnimatedBuilder calls its builder on every animation tick. Pass unchanging parts of the tree through the child argument so Flutter reuses them instead of rebuilding them.
AnimatedBuilder(
animation: _controller,
child: Container(width: 80, height: 80, color: Colors.indigo),
builder: (context, child) {
return Transform.rotate(angle: _controller.value * 6.283, child: child);
},
)Ready-made transitions
Flutter provides transition widgets that accept an Animation directly and repaint their child without a full rebuild.
| Widget | Animated property | Animation type |
|---|---|---|
| FadeTransition | Opacity | Animation<double> |
| ScaleTransition | Scale | Animation<double> |
| SlideTransition | Position offset | Animation<Offset> |
Playing, reversing, repeating and listening to status
The controller exposes forward(), reverse(), and repeat(). Use addStatusListener to react when an animation completes or returns to the start.
_controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_controller.reverse();
} else if (status == AnimationStatus.dismissed) {
_controller.forward();
}
});Disposing controllers and keeping animations cheap
A controller holds a ticker tied to its State. Call dispose() in the State's dispose() method. Skipping it keeps the ticker alive after the widget is gone.
Staggered animations with Interval
An Interval restricts a curve to a slice of the controller's timeline. Two animations on one controller can then run one after the other. For example, Interval(0.0, 0.5) runs a fade during the first half, and Interval(0.4, 1.0) runs a slide during the rest.
Choosing the right approach
Use an implicit widget when a property moves to a target value. Use an explicit controller when you need repetition, reversal, status events, or several synchronized effects.
Exercise
In the staggered example, at what controller value does the fade finish?
Show answer
At 0.5, the midpoint of the controller's timeline, because the Interval is Interval(0.0, 0.5).