Sub topic 8
Animating a painter with the repaint listenable
This chapter assumes you know Dart classes, Flutter widget trees, layout constraints, and the basics of AnimationController. An AnimationController produces changing values over time. A CustomPainter can listen to those changes and repaint without rebuilding the surrounding widget tree.
Why use the repaint listenable?
A painter can receive an animation through the repaint parameter of its superclass:
super(repaint: animation)The animation becomes a Listenable. Whenever its value changes, Flutter asks the painter to paint again. This keeps animation work in the paint phase instead of calling setState for every frame.
The widget still needs to create and dispose of the controller. The painter only draws the current animation value.
Build a rotating painter
The following example rotates a square around the center of a CustomPaint widget.
import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: SpinnerPage()));
}
class SpinnerPage extends StatefulWidget {
const SpinnerPage({super.key});
@override
State<SpinnerPage> createState() => _SpinnerPageState();
}
class _SpinnerPageState extends State<SpinnerPage>
with SingleTickerProviderStateMixin {
late final AnimationController _controller =
AnimationController(
vsync: this,
duration: const Duration(seconds: 2),
)..repeat();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CustomPaint(
size: const Size(200, 200),
painter: SpinnerPainter(_controller),
),
),
);
}
}
class SpinnerPainter extends CustomPainter {
SpinnerPainter(this.animation) : super(repaint: animation);
final Animation<double> animation;
@override
void paint(Canvas canvas, Size size) {
canvas.save();
canvas.translate(size.width / 2, size.height / 2);
canvas.rotate(animation.value * 2 * math.pi);
final paint = Paint()..color = Colors.orange;
canvas.drawRect(
Rect.fromCenter(
center: Offset.zero,
width: 80,
height: 80,
),
paint,
);
canvas.restore();
}
@override
bool shouldRepaint(covariant SpinnerPainter oldDelegate) {
return oldDelegate.animation != animation;
}
}animation.value ranges from 0.0 to 1.0 during one controller cycle. Multiplying it by 2 * math.pi converts that progress into one full rotation.
save stores the canvas state. translate moves the origin to the center, and rotate rotates around that new origin. restore returns the canvas to its original state so later drawing is not affected.
Repaint rules and common errors
shouldRepaint is used when Flutter receives a new painter object. It compares the old delegate with the new one. The repaint listenable handles changes inside the same painter while the animation runs.
Avoid creating an animation controller inside paint. Avoid calling setState on every tick when the painter can listen directly. Always dispose of the controller, or the ticker can continue using resources after the widget is removed.
Exercise
Why are canvas.save() and canvas.restore() paired around the spinner's transform?
Show answer
They keep the translation and rotation local to this painter operation. Without restore(), later drawing on the same canvas could inherit the spinner's transformed coordinate system.