Question #305MediumWidgets & UI

What is the use of Ticker in Flutter?

#flutter#animation

Answer

Overview

A Ticker in Flutter is an object that fires a callback once per frame (typically 60 times per second). It is the foundation of Flutter's animation system — every animation uses a Ticker under the hood.


How Ticker Works

text
Frame 1 (0ms)   → Ticker fires → AnimationController updates → Widget rebuilds
Frame 2 (16ms)  → Ticker fires → AnimationController updates → Widget rebuilds
Frame 3 (32ms)  → Ticker fires → AnimationController updates → Widget rebuilds
...

A Ticker sends the elapsed time since it started to its callback on every frame.


SingleTickerProviderStateMixin

The most common way to use a Ticker in Flutter — automatically provides a

text
vsync
for
text
AnimationController
:

dart
class MyAnimatedWidget extends StatefulWidget {
  
  _MyAnimatedWidgetState createState() => _MyAnimatedWidgetState();
}

class _MyAnimatedWidgetState extends State<MyAnimatedWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this, // ← 'this' is the Ticker provider
      duration: Duration(seconds: 2),
    )..repeat();   // Loop forever
  }

  
  void dispose() {
    _controller.dispose(); // Always dispose!
    super.dispose();
  }

  
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: _controller,
      child: Icon(Icons.refresh, size: 48),
    );
  }
}

TickerProviderStateMixin (Multiple Animations)

When you need more than one AnimationController:

dart
class _MyState extends State<MyWidget>
    with TickerProviderStateMixin { // ← Note: no "Single"
  late AnimationController _controller1;
  late AnimationController _controller2;

  
  void initState() {
    super.initState();
    _controller1 = AnimationController(vsync: this, duration: Duration(seconds: 1));
    _controller2 = AnimationController(vsync: this, duration: Duration(milliseconds: 500));
  }

  
  void dispose() {
    _controller1.dispose();
    _controller2.dispose();
    super.dispose();
  }
}

Ticker vs AnimationController

ConceptRole
TickerLow-level: fires once per frame
AnimationControllerHigh-level: uses Ticker to drive animation values
vsyncConnects AnimationController to a Ticker

Why vsync Matters

text
vsync
prevents animations from updating when the widget is off-screen (e.g., on a different tab), saving battery and CPU:

dart
// ✅ With vsync — animation pauses when off-screen
AnimationController(vsync: this, duration: Duration(seconds: 1))

// ❌ Without vsync — animation keeps running even when not visible (bad!)

Summary: A

text
Ticker
fires a callback every frame. Use
text
SingleTickerProviderStateMixin
for single animations and
text
TickerProviderStateMixin
for multiple animations. Always
text
dispose()
your controllers.