Sub topic 17
Staggered animations with Interval
A staggered animation is a set of effects that start and finish at different moments inside one overall animation. A card might fade in first, then slide up while the fade is still finishing. A list might reveal its rows one after another.
This chapter assumes you can already build an AnimationController with a TickerProvider mixin. It also assumes you know Tween, CurvedAnimation, and how AnimatedBuilder or the transition widgets consume an Animation.
The idea: one controller, many time windows
You do not need one controller per effect. A single controller runs from 0.0 to 1.0, and each effect watches only a slice of that range. Interval defines the slice.
Interval(begin, end, curve: ...) is a Curve. It takes the controller's value t and does three things:
- It rescales
tso thatbeginmaps to 0.0 andendmaps to 1.0. - It clamps the result. Before
beginthe output is 0.0, and afterendit is 1.0. - It applies the inner
curveto the rescaled value.
So an effect has finished, and stays finished, once the controller passes its end. It does not start until the controller reaches its begin.
Turning fractions into time
The interval bounds are fractions of the controller's duration. They are not milliseconds. The staggered example from the plan uses a 1200 ms controller:
| Effect | Interval | Starts at | Ends at |
|---|---|---|---|
| Fade in | 0.0 to 0.5 | 0 ms | 600 ms |
| Slide up | 0.4 to 1.0 | 480 ms | 1200 ms |
The two effects overlap from 480 ms to 600 ms. Overlap is a choice. Set the second begin equal to the first end for strict sequencing, or set it lower for a smoother hand-off.
If you change the controller's duration, every effect scales with it. The relative timing stays the same. That is the main advantage of keeping all timing in one place.
The full pattern for a single widget
The pattern has three steps:
- Create one controller.
- Wrap it in a
CurvedAnimationwith anIntervalfor each effect. - Feed the result to a
Tweenor directly to a transition widget.
For the slide, the order matters. The Interval goes inside the CurvedAnimation, and the Tween wraps that:
_slide = Tween<Offset>(
begin: const Offset(0, 0.5),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.4, 1.0, curve: Curves.easeOut),
),
);Curves.easeOut here applies to the rescaled 0 to 1 value inside the interval. It does not apply to the whole 1200 ms. That is why a curve inside an Interval feels "local" to its effect.
Staggering a list
Staggering many items follows the same rule. Give each item a window that starts a little later than the previous one. The code below offsets each start by 0.15 and gives every window a length of 0.4. With four items, the last window ends at 0.45 + 0.4 = 0.85, which fits inside the controller's range.
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(body: Center(child: StaggeredList())),
),
);
}
class StaggeredList extends StatefulWidget {
const StaggeredList({super.key});
@override
State<StaggeredList> createState() => _StaggeredListState();
}
class _StaggeredListState extends State<StaggeredList>
with SingleTickerProviderStateMixin {
static const _labels = ['Inbox', 'Drafts', 'Sent', 'Archive'];
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1000),
);
late final List<Animation<double>> _animations = List.generate(
_labels.length,
(index) {
final start = index * 0.15;
return CurvedAnimation(
parent: _controller,
curve: Interval(start, start + 0.4, curve: Curves.easeOut),
);
},
);
@override
void initState() {
super.initState();
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
for (var i = 0; i < _labels.length; i++)
FadeTransition(
opacity: _animations[i],
child: SlideTransition(
position: _animations[i].drive(
Tween<Offset>(
begin: const Offset(0.3, 0),
end: Offset.zero,
),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(_labels[i]),
),
),
),
TextButton(
onPressed: () => _controller.forward(from: 0),
child: const Text('Replay'),
),
],
);
}
}The CurvedAnimation objects are created once, in a late final field initializer. Creating them inside build would make a new set on every rebuild. The transition widgets already rebuild only their own part of the tree, so the Column is not rebuilt on each tick.
Common errors and edge cases
- Bounds outside 0.0 to 1.0.
Intervalasserts thatbeginandendlie in that range and thatendis not less thanbegin. A computed window such asstart + 0.4can pass 1.0 when you add items. Compute the step from the item count instead, for examplestep = 0.6 / (count - 1). - A window that is too short. A window of 0.05 on a 400 ms controller lasts only 20 ms, and the effect looks like a jump. Check the window length in milliseconds, not just as a fraction.
- Expecting gaps to hold the start value. Before
beginthe interval returns 0.0, so a fade sits at its start value (fully transparent). If a widget should be hidden before its turn, 0.0 opacity does that. ATweenwith a visible start value would show the start state immediately instead. - Reverse playback. On
reverse()the controller value falls from 1.0 to 0.0, and each interval is read backward. The effect with the latest window therefore plays first. To use different timing on the way back, passreverseCurvetoCurvedAnimation. - Forgetting to restart. After
forward()completes, calling it again does nothing because the value is already 1.0. Useforward(from: 0)to replay. - Missing
dispose. The controller still needsdispose. SeveralCurvedAnimationobjects over one controller do not change that.
When to use it
Choose staggering when several effects belong to one logical event, such as a screen entrance or a list reveal, and must stay synchronized. If the effects are independent, for example two unrelated buttons, give each its own implicit animation or controller. If you only need a delay for one effect, a single Interval with begin above 0.0 is enough.
Exercise
A controller has a duration of 2000 ms. An effect uses Interval(0.25, 0.75, curve: Curves.easeIn). During which milliseconds of the playback does the effect move, and what is its value at 400 ms and at 1800 ms?
Show answer
The effect moves from 500 ms to 1500 ms, because 0.25 × 2000 = 500 and 0.75 × 2000 = 1500. At 400 ms the controller value is 0.2, which is before begin, so the interval returns 0.0. At 1800 ms the controller value is 0.9, which is after end, so it returns 1.0. In both cases the inner easeIn curve is not consulted.
Exercise
You add a fifth label to the StaggeredList example without changing the offsets of 0.15 and the window length of 0.4. What goes wrong, and how do you fix it?
Show answer
The fifth item's window is Interval(0.6, 1.0). That is still valid, because 0.6 + 0.4 equals 1.0 exactly. A sixth item would use Interval(0.75, 1.15), and the assertion fails because end exceeds 1.0. The fix is to compute the step from the item count so that the last window ends at 1.0, for example step = (1 - window) / (count - 1) and start = index * step. Alternatively, lengthen the controller's duration and shorten the window.