Sub topic 13
Rebuilding efficiently with AnimatedBuilder
This chapter assumes you know Dart classes, StatefulWidget, setState, and the basic role of an AnimationController. You will learn how AnimatedBuilder connects a controller to a widget while limiting which part of the widget tree rebuilds.
What AnimatedBuilder does
An AnimationController changes its value on every animation frame. A widget that depends on that value must rebuild to show the new frame.
AnimatedBuilder listens to an animation and calls its builder function whenever the animation changes. The builder receives the current BuildContext and an optional child.
The child is useful for content that does not depend on the animation. Flutter builds that content once and passes the same widget back to the builder on every frame.
Reuse static content with child
The following widget rotates a square continuously. The Transform.rotate is rebuilt for each tick, but the square passed as child is reused.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: SpinningSquare()));
}
class SpinningSquare extends StatefulWidget {
const SpinningSquare({super.key});
@override
State<SpinningSquare> createState() => _SpinningSquareState();
}
class _SpinningSquareState extends State<SpinningSquare>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 2),
);
@override
void initState() {
super.initState();
_controller.repeat();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimatedBuilder(
animation: _controller,
child: Container(
width: 80,
height: 80,
color: Colors.indigo,
),
builder: (context, child) {
return Transform.rotate(
angle: _controller.value * 2 * 3.141592653589793,
child: child,
);
},
),
),
);
}
}The animation parameter identifies the object that triggers rebuilds. The builder function reads _controller.value and creates the changing part. The child parameter holds the square because its size and color never change.
Without the child parameter, the square would still usually work correctly. However, Flutter would recreate that widget description each time the builder runs. Keeping static content outside the builder makes the intended rebuild boundary clear and can reduce unnecessary work in a larger subtree.
When to use AnimatedBuilder
Use AnimatedBuilder when:
- An explicit animation controls a custom property.
- You need one controller to drive several widgets.
- You want to reuse a static subtree.
- No specialized transition widget matches the effect.
Use a built-in transition, such as FadeTransition or ScaleTransition, when it already expresses the effect clearly. Use AnimatedBuilder for effects such as rotation, color changes, or combined transforms.
Exercise
Why should a static widget be passed through AnimatedBuilder's child parameter instead of created inside builder?
Show answer
The child is reused across animation frames, so the builder only creates the parts that depend on the changing animation value.