Sub topic 2
When implicit animations are not enough
This chapter assumes you know StatefulWidget, setState, and the build method. You should also have used an implicit widget such as AnimatedContainer.
When implicit animations are not enough
Implicit animations work well when a property changes in response to a state change and you want Flutter to move smoothly between the old and new values. They stop being enough when you need to start, stop, reverse, or repeat an animation on your own schedule, or when several properties must follow one shared timeline. Explicit animations give you that control.
Explicit animations: taking control
With an explicit animation, you own the timeline. You create an AnimationController, decide when it runs, and read its value on every frame. Flutter still rebuilds the affected widgets, but you decide what changes and when.
AnimationController
An AnimationController is an object that produces a value between 0.0 and 1.0 over a set duration. You call forward(), reverse(), or repeat() to drive it. You must call dispose() on it when the widget is removed, because a running controller keeps using resources.
Tickers and vsync
A ticker is a timer that fires once per screen frame. The vsync argument tells the controller which ticker to use, so the animation runs only while the widget is visible. Adding SingleTickerProviderStateMixin to your state class supplies that ticker. Pass this as vsync.
Mapping values with Tween
A Tween maps the controller's 0.0 to 1.0 range onto any range you need. Calling animate(controller) returns an Animation whose value is the mapped number. The example below grows a box from 80 to 160 pixels.
import 'package:flutter/material.dart';
class GrowBox extends StatefulWidget {
const GrowBox({super.key});
@override
State<GrowBox> createState() => _GrowBoxState();
}
class _GrowBoxState extends State<GrowBox>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _size;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
_size = Tween<double>(begin: 80, end: 160).animate(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
AnimatedBuilder(
animation: _size,
builder: (context, child) => Container(
width: _size.value,
height: _size.value,
color: Colors.teal,
),
),
ElevatedButton(
onPressed: () => _controller.forward(from: 0),
child: const Text('Play'),
),
],
);
}
}Exercise
The button in the example grows the box once. How would you make the box shrink back to 80 pixels after it reaches 160?
Show answer
Add a status listener with _controller.addStatusListener((status) { if (status == AnimationStatus.completed) _controller.reverse(); }); in initState, and call _controller.removeStatusListener in dispose if you want to be strict about cleanup. Alternatively, call _controller.reverse() from the button handler after the forward run finishes.