Sub topic 8
Explicit animations: taking control
Prerequisites and the core idea
This chapter assumes that you know Dart classes, mixins, generics, and closures. You should also know how StatefulWidget, setState, and the build method work.
An explicit animation gives your code direct control over motion. You decide when the animation starts, stops, reverses, or repeats. This differs from an implicit animation, where Flutter notices a changed property and animates toward the new value automatically.
An explicit animation usually has three parts:
- An
AnimationControllerproduces a changing value from0.0to1.0. - An
Animation<T>maps that progress to a useful type, such as a size, color, angle, or position. - A widget reads the value and displays the current frame.
The controller does not paint anything by itself. It only tracks progress over time. A listener or an animation widget then updates the visible part of the widget tree.
The explicit animation model
A controller needs a ticker. A ticker sends timing signals while a widget's animation is active. The vsync parameter connects the controller to the widget's lifecycle and helps avoid producing frames when they are not needed.
This example fades and scales a logo when the user taps it:
import 'package:flutter/material.dart';
class ExplicitLogo extends StatefulWidget {
const ExplicitLogo({super.key});
@override
State<ExplicitLogo> createState() => _ExplicitLogoState();
}
class _ExplicitLogoState extends State<ExplicitLogo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _opacity;
late final Animation<double> _scale;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 600),
);
_opacity = Tween<double>(begin: 0.3, end: 1.0).animate(_controller);
_scale = Tween<double>(begin: 0.8, end: 1.0).animate(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _controller.forward(from: 0),
child: FadeTransition(
opacity: _opacity,
child: ScaleTransition(
scale: _scale,
child: const FlutterLogo(size: 100),
),
),
);
}
}SingleTickerProviderStateMixin supplies the ticker required by one controller. The controller progresses for 600 milliseconds. Each Tween converts the controller's progress into a different range. Both animations therefore run together but can affect different properties.
Choosing explicit control
Use an explicit animation when the timing or playback is part of the behavior. Good examples include a looping pulse, a sequence with several stages, or an animation that reverses when a condition changes.
| Requirement | Suitable approach |
|---|---|
| Animate one property after it changes | Implicit animation |
| Start, stop, reverse, or repeat motion | Explicit animation |
| Coordinate several effects on one timeline | Explicit animation |
Exercise
Why does the example use both an AnimationController and a Tween?
Show answer
The controller supplies time-based progress from 0 to 1, while each Tween maps that progress to a property-specific range such as opacity or scale.