Sub topic 15
Playing, Reversing, Repeating and Listening to Status
This chapter assumes you know Dart classes, mixins, closures, StatefulWidget, build, and basic AnimationController setup. An animation controller produces a value from 0.0 to 1.0. Playback methods move that value through its range.
Playback methods
Use these methods to control an AnimationController:
| Method | Effect |
|---|---|
| `forward()` | Moves from the current value toward `1.0` |
| `reverse()` | Moves from the current value toward `0.0` |
| `repeat()` | Runs continuously between the bounds |
| `stop()` | Pauses at the current value |
| `reset()` | Sets the value to `0.0` without playing |
forward() and reverse() do not always start at an endpoint. For example, calling reverse() after stop() continues from the current value toward zero.
Listening to animation status
AnimationStatus describes the controller's playback state:
dismissed: the value is at0.0.forward: the value is moving toward1.0.completed: the value is at1.0.reverse: the value is moving toward0.0.
A status listener is useful when one animation should trigger another action. For example, an animation can automatically reverse after reaching its end.
The following complete app displays the current status and provides playback controls.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PlaybackDemo()));
}
class PlaybackDemo extends StatefulWidget {
const PlaybackDemo({super.key});
@override
State<PlaybackDemo> createState() => _PlaybackDemoState();
}
class _PlaybackDemoState extends State<PlaybackDemo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _scale;
AnimationStatus _status = AnimationStatus.dismissed;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 2),
);
_scale = Tween<double>(begin: 0.7, end: 1.3).animate(_controller);
_controller.addStatusListener(_handleStatus);
}
void _handleStatus(AnimationStatus status) {
if (mounted) {
setState(() => _status = status);
}
}
@override
void dispose() {
_controller.removeStatusListener(_handleStatus);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Playback control')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ScaleTransition(
scale: _scale,
child: const Icon(Icons.favorite, size: 80, color: Colors.red),
),
const SizedBox(height: 24),
Text('Status: ${_status.name}'),
Wrap(
spacing: 8,
children: [
ElevatedButton(
onPressed: _controller.forward,
child: const Text('Forward'),
),
ElevatedButton(
onPressed: _controller.reverse,
child: const Text('Reverse'),
),
ElevatedButton(
onPressed: () =>
_controller.repeat(reverse: true),
child: const Text('Repeat'),
),
ElevatedButton(
onPressed: _controller.stop,
child: const Text('Stop'),
),
ElevatedButton(
onPressed: _controller.reset,
child: const Text('Reset'),
),
],
),
],
),
),
);
}
}Repeating and chaining animations
repeat() continues until you call stop() or dispose of the controller. With reverse: true, the controller travels from zero to one and back again instead of jumping from one to zero.
A status listener can chain playback:
_controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_controller.reverse();
} else if (status == AnimationStatus.dismissed) {
_controller.forward();
}
});This creates a continuous back-and-forth animation. Remove the listener when it is no longer needed, especially when the controller has multiple listeners.
Exercise
Which method should you call to pause an animation without changing its current position?
Show answer
Call stop(). The controller keeps its current value, so a later forward() or reverse() continues from that position.