Sub topic 17
Staggered animations with `Interval`
Assume you know StatefulWidget, AnimationController, Tween, CurvedAnimation, and transition widgets. A staggered animation is one animation made from several effects that start or finish at different times.
A single AnimationController provides a timeline. Its value moves from 0.0 to 1.0. An Interval selects a section of that timeline for one effect.
For example:
- An opacity animation can run from
0.0to0.5. - A slide animation can run from
0.4to1.0. - The two effects overlap between
0.4and0.5.
Mapping effects to a timeline
Interval(begin, end) remaps its selected section to the full animation range. While the controller is before begin, the interval stays at 0.0. After end, it stays at 1.0.
The following widget fades text in first, then moves it into place. The effects overlap briefly.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: StaggeredDemo()));
}
class StaggeredDemo extends StatefulWidget {
const StaggeredDemo({super.key});
@override
State<StaggeredDemo> createState() => _StaggeredDemoState();
}
class _StaggeredDemoState extends State<StaggeredDemo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _opacity;
late final Animation<Offset> _slide;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 1200),
);
_opacity = CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5, curve: Curves.easeIn),
);
_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),
),
);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FadeTransition(
opacity: _opacity,
child: SlideTransition(
position: _slide,
child: const Text(
'Welcome',
style: TextStyle(fontSize: 32),
),
),
),
),
);
}
}The controller runs for 1,200 milliseconds. The opacity effect occupies the first 600 milliseconds. The slide begins at 40 percent, or 480 milliseconds, and ends when the controller reaches 1.0.
Designing staggered effects
Use one controller when the effects belong to one coordinated sequence. Adjust each interval to control order and overlap.
Use non-overlapping intervals for clearly separated steps. For example, use Interval(0.0, 0.33) for a title, Interval(0.33, 0.66) for an image, and Interval(0.66, 1.0) for buttons.
Use overlapping intervals when the transition should feel continuous. Avoid too many tiny intervals because the sequence becomes difficult to tune and test.
Exercise
If an animation has a 2-second duration and uses Interval(0.25, 0.75), when does the effect begin and end?
Show answer
It begins at 0.5 seconds and ends at 1.5 seconds. The interval uses the middle half of the controller's timeline.