Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 73 ยท Animation and graphics

Explicit Animations and AnimationController

Take full control of Flutter animation: AnimationController, vsync and tickers, Tween, CurvedAnimation, AnimatedBuilder and staggered animations with Intervals.

Implicit animations are great until you need more: an animation that loops forever, that you can pause, that reverses when a drag ends, or that moves three things with different timings. For these you use explicit animations, where you own an AnimationController and decide when it runs.

This lesson explains the pieces and how they fit together: the controller and its ticker, vsync, Tween, CurvedAnimation, AnimatedBuilder, and staggered animations with Interval. It builds on StatefulWidget and setState, because a controller must be created in initState and released in dispose.

Real vs stand-in. The Dart is real Flutter code, read-only here, because the site cannot run Flutter. The live examples are JavaScript stand-ins: a fake ticker that delivers frames at exact times, a controller, tweens and an interval, so every run prints the same numbers.

The explicit animation chain

Ticker Calls you once per frame.
Controller A value from 0.0 to 1.0.
Curve Reshapes the progress.
Tween Maps 0 to 1 onto real values.
1 / 2

The AnimationController

An AnimationController is an Animation<double> whose value moves from lowerBound (default 0.0) to upperBound (default 1.0) over a duration. It does not know about widths, colors or curves; it only produces progress.

DART
class SpinningLogo extends StatefulWidget {
  const SpinningLogo({super.key});

  @override
  State<SpinningLogo> createState() => _SpinningLogoState();
}

class _SpinningLogoState extends State<SpinningLogo>
    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 RotationTransition(
      turns: _controller,
      child: const FlutterLogo(size: 80),
    );
  }
}

Three things to notice:

  1. with SingleTickerProviderStateMixin makes the State a TickerProvider. Use TickerProviderStateMixin (no "Single") when the State owns more than one controller.
  2. vsync: this links the controller to the screen. A ticker is told by the engine when a new frame is about to be drawn, and the controller advances only then. A ticker is muted when the widget is off-screen or the app is in the background, so hidden animations do not waste battery. This is what vsync buys you.
  3. dispose is not optional. A controller you forget to dispose leaks its ticker, and Flutter reports it in debug mode.

Controller methods: forward(), reverse(), repeat(reverse: true), stop(), reset(), animateTo(0.5), fling(), and the value property. Most return a TickerFuture, so await _controller.forward(); waits for the animation to finish.

Stand-in: ticker and controller

A real ticker gets a timestamp from the engine on each frame. This stand-in delivers frames at times you choose, so we can print the progress the controller would report.

JS A controller driven by a fake ticker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The controller reports only 0.00 to 1.00. Everything else (distance, color, angle) is mapped from that number. Reversing from 1.0 starts the countdown from where the value is now, not from zero.

Tween and CurvedAnimation

A Tween<T> maps the range 0 to 1 onto any begin and end, and .animate(parent) produces an Animation<T> that follows the parent controller:

DART
late final Animation<double> _size =
    Tween<double>(begin: 80, end: 200).animate(_controller);

late final Animation<Color?> _color =
    ColorTween(begin: Colors.blue, end: Colors.red).animate(_controller);

To change the speed profile, wrap the controller in a CurvedAnimation first:

DART
late final Animation<double> _eased = CurvedAnimation(
  parent: _controller,
  curve: Curves.easeOutBack,
  reverseCurve: Curves.easeIn,
);

late final Animation<double> _size =
    Tween<double>(begin: 80, end: 200).animate(_eased);

The order is always controller โ†’ curve โ†’ tween. Because CurvedAnimation is also an Animation, you can nest them, and several tweens can share one controller. Curves like elasticOut or easeOutBack may produce values beyond 0 to 1, which is fine for a tween but not for something like opacity, so clamp it or choose another curve. For CurvedAnimation, call dispose() on it too in recent Flutter versions.

Rebuilding with AnimatedBuilder

The controller changes 60 or more times per second. Calling setState for each tick works, but it rebuilds the whole State. AnimatedBuilder listens to an animation and rebuilds only its builder:

DART
AnimatedBuilder(
  animation: _controller,
  child: const Icon(Icons.favorite, color: Colors.white, size: 40),   // built once
  builder: (context, child) {
    return Container(
      width: _size.value,
      height: _size.value,
      decoration: BoxDecoration(
        color: _color.value,
        borderRadius: BorderRadius.circular(_size.value / 4),
      ),
      child: child,
    );
  },
)

The child parameter is the performance trick from the previous lesson: widgets that do not depend on the animation are built once and passed through. There are also ready-made *Transition widgets (FadeTransition, ScaleTransition, SlideTransition, RotationTransition, SizeTransition) that take an Animation and do this internally for common properties. Prefer them for simple cases.

JS Tween and curve between controller and value

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both columns start at 80 and end at 200. The eased column is already at 185 after half the time, because the curve reshaped progress before the tween turned it into pixels.

Staggered animations

A staggered animation moves several properties with different timing from one controller. Each tween is given an Interval, a window within the 0 to 1 range during which it runs:

DART
late final Animation<double> _fade = Tween<double>(begin: 0, end: 1).animate(
  CurvedAnimation(
    parent: _controller,
    curve: const Interval(0.0, 0.4, curve: Curves.easeOut),
  ),
);

late final Animation<Offset> _slide = Tween<Offset>(
  begin: const Offset(0, 0.3),
  end: Offset.zero,
).animate(
  CurvedAnimation(
    parent: _controller,
    curve: const Interval(0.3, 0.8, curve: Curves.easeOut),
  ),
);

late final Animation<double> _scale = Tween<double>(begin: 0.8, end: 1).animate(
  CurvedAnimation(
    parent: _controller,
    curve: const Interval(0.6, 1.0, curve: Curves.elasticOut),
  ),
);

With a 1 second controller, the fade finishes at 400 ms, the slide runs from 300 to 800 ms and the scale from 600 ms to the end. Outside its interval a tween holds its begin value (before) or end value (after). A list that animates its items in one after another uses the same trick: item i gets Interval(i * 0.1, i * 0.1 + 0.4).

JS Intervals on one shared controller

Press Run (or Ctrl + Enter) to see the console output here.

No output.

At 400 ms the fade is complete while the slide is only 20% done and the scale has not started. One controller, three overlapping animations.

Controlling animations

Common patterns with a controller:

  • Play/pause: controller.isAnimating ? controller.stop() : controller.forward().
  • Toggle: call forward() when status == AnimationStatus.dismissed and reverse() otherwise.
  • React to the end: controller.addStatusListener((s) { if (s == AnimationStatus.completed) controller.reverse(); }) makes a ping-pong, or just use repeat(reverse: true).
  • Follow a drag: set controller.value from the gesture delta and call fling() on release (Gestures and Input Handling).

Remember mounted and dispose rules: await a forward() future and check mounted before touching the UI. Page-to-page motion with Hero is the subject of Hero and Page Transitions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does vsync: this give an AnimationController?

    The TickerProvider supplies tickers tied to screen frames, which are muted when the widget is hidden.
  2. 2. What is the correct order to chain the pieces?

    The controller makes progress, a curve reshapes it, and the tween maps it onto real values.
  3. 3. Which mixin do you use when a State owns two AnimationControllers?

    SingleTickerProviderStateMixin supports one ticker only. TickerProviderStateMixin supports many.
  4. 4. Why use AnimatedBuilder with a child parameter?

    Only the animated part is rebuilt per frame; the child passes through unchanged.
  5. 5. In a staggered animation, what does Interval(0.3, 0.8) mean?

    An Interval gives a window of the controller's progress; outside it the tween holds its begin or end value.

Summary

  • An AnimationController produces progress from 0 to 1 using a ticker supplied through vsync; create it in initState and dispose it.
  • Chain controller, CurvedAnimation and Tween to turn progress into sizes, colors and offsets.
  • Use AnimatedBuilder (or a *Transition widget) to rebuild only what animates, passing static widgets as child.
  • Staggered animations use one controller and an Interval per tween.
  • Prefer implicit animations when you do not need looping, pausing, reversing or drag control.

Next: Hero and Page Transitions