Sub topic 16
Disposing Controllers and Keeping Animations Cheap
This chapter assumes you know Dart classes and mixins, Flutter’s build method, StatefulWidget, setState, and basic explicit animations with AnimationController.
An explicit animation has an ongoing resource: its controller owns a ticker that produces frame updates. The controller must be stopped or disposed when its State leaves the widget tree. Otherwise, it can keep requesting frames, retain objects, or produce errors after the widget is gone.
Dispose every controller you create
Create a controller in initState, not in build. The build method may run many times. Creating a controller there would restart the animation and allocate unnecessary objects.
Dispose the controller in dispose. Call super.dispose() after releasing your resources.
import 'package:flutter/material.dart';
class PulsingLogo extends StatefulWidget {
const PulsingLogo({super.key});
@override
State<PulsingLogo> createState() => _PulsingLogoState();
}
class _PulsingLogoState extends State<PulsingLogo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _scale;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 900),
)..repeat(reverse: true);
_scale = Tween<double>(begin: 0.9, end: 1.1).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
scale: _scale,
child: const FlutterLogo(size: 96),
);
}
}SingleTickerProviderStateMixin supplies a ticker for one controller. If a state needs multiple controllers, use TickerProviderStateMixin instead.
Reduce work on every frame
An animation can update 60 or more times per second. Keep the part rebuilt on each tick small.
AnimatedBuilder lets you separate the changing part from a static child. Flutter reuses the child instead of rebuilding it for every frame.
AnimatedBuilder(
animation: _controller,
child: const Text('Loading'),
builder: (context, child) {
return Transform.rotate(
angle: _controller.value * 6.28318,
child: child,
);
},
)Prefer transform and opacity animations when possible. They often avoid expensive layout or painting work. Avoid rebuilding a large page, performing network requests, or creating complex object graphs inside an animation builder.
Understand lifecycle and visibility
A controller can continue ticking while its widget is covered or not useful. TickerMode can disable tickers in a subtree, which is useful for inactive screens or tabs.
| Practice | Benefit |
|---|---|
| Create once and dispose once | Prevents leaks and repeated tickers |
| Pass static content as `child` | Avoids rebuilding unchanged widgets |
| Animate transforms or opacity | Usually reduces layout work |
Exercise
Where should an AnimationController be created and disposed?
Show answer
Create it in initState and dispose it in the State object's dispose method.