Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 73 ยท Animation and graphics

Lottie and Rive Animations

Add designer-made animations to Flutter with the lottie package and Rive state machines: loading files, controlling playback, inputs, performance and when to choose each.

Code-driven animations (implicit and explicit) are perfect for fades and slides, but a character that waves, a checkmark that draws itself or a confetti burst would take hundreds of lines to build by hand. Designers make these in tools like After Effects and Rive, and Flutter plays the exported files. Two formats dominate:

  • Lottie: a JSON (or compressed .lottie) description of an After Effects animation. It is a timeline: you play it forward, backward, loop it, or scrub it to a point.
  • Rive: a .riv file from the Rive editor that can contain a state machine with inputs, so the artwork reacts to your app (a bear that looks happy or sad, a toggle that morphs).

Real vs stand-in. The Dart is real Flutter code and read-only, since the site cannot run a Flutter app or load these files. Rive's runtime API has changed between major versions of the rive package, so the Rive code below shows the concept using the widely documented classic API; check the package page for the version you install. The live examples are JavaScript stand-ins of a Lottie timeline and a Rive-style state machine.

Lottie

Add the lottie package, put the exported JSON in your assets folder, and declare it in pubspec.yaml (declaring assets).

DART
// Plays once and loops by default.
Lottie.asset('assets/animations/success.json', width: 160, height: 160)

// From the network (cached by the package):
Lottie.network('https://example.com/loader.json')

// Play once, no looping:
Lottie.asset('assets/animations/success.json', repeat: false)

For control, give Lottie an AnimationController and set its duration from the loaded composition:

DART
class _SuccessState extends State<Success> with SingleTickerProviderStateMixin {
  late final AnimationController _controller = AnimationController(vsync: this);

  @override
  Widget build(BuildContext context) {
    return Lottie.asset(
      'assets/animations/success.json',
      controller: _controller,
      onLoaded: (composition) {
        _controller
          ..duration = composition.duration
          ..forward();
      },
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

From here everything you know about AnimationController applies: forward, reverse, repeat, animateTo(0.5) to stop halfway, or _controller.value = 0.3 to show a single frame. That makes a Lottie useful as a progress indicator you drive from your own data, such as a download percentage.

Stand-in: a Lottie timeline

A Lottie file stores a frame rate (fr), an in-point (ip) and out-point (op). Duration is (op - ip) / fr seconds, and the controller's value (0 to 1) picks the frame.

JS Controller value to frame number

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

No output.

Rive and state machines

A Rive file contains artboards, animations, and optionally state machines. A state machine has named inputs (a boolean, a number, a trigger) that you set from Dart; the artwork decides how to blend and transition between its animations. The logic of "when happy, wave" lives in the file, so the designer can change it without touching your code.

DART
// Classic runtime API (rive 0.13 and earlier); newer releases use a different controller API.
class _BearState extends State<Bear> {
  SMIBool? _isHappy;
  SMITrigger? _wave;

  void _onRiveInit(Artboard artboard) {
    final controller = StateMachineController.fromArtboard(artboard, 'Main');
    if (controller == null) return;               // wrong state machine name
    artboard.addController(controller);
    _isHappy = controller.findInput<bool>('isHappy') as SMIBool?;
    _wave = controller.findInput<bool>('wave') as SMITrigger?;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 200,
          child: RiveAnimation.asset(
            'assets/rive/bear.riv',
            stateMachines: const ['Main'],
            onInit: _onRiveInit,
          ),
        ),
        Switch(value: _isHappy?.value ?? false, onChanged: (v) => setState(() => _isHappy?.value = v)),
        ElevatedButton(onPressed: () => _wave?.fire(), child: const Text('Wave')),
      ],
    );
  }
}

The names ('Main', 'isHappy', 'wave') must match the Rive file exactly. If an input is null, the name is wrong or the file has not loaded yet, so handle that. Rive animations are rendered by Rive's own runtime and can stay smooth under heavy interaction, which is why they are popular for tappable characters, onboarding and game UI.

JS A state machine with inputs and a trigger

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

No output.

Choosing between them

Lottie Rive
Made in After Effects (Bodymovin export) or LottieFiles tools The Rive editor
Shape A timeline you play and scrub Timelines plus interactive state machines
Best for Success checks, loaders, illustrations, confetti Interactive characters, toggles, game UI
Size Can be large with many layers or embedded images Usually small (vector, binary)

Performance and practical tips

  • File size matters. A heavy Lottie with embedded images inflates the app and takes time to parse. Ask designers for simple vector layers, or use the compressed .lottie format.
  • Not every After Effects feature is supported in every player. Test the exact file on the target platforms.
  • Pause what is off screen. An endless animation on a hidden tab still costs battery; stop the controller when the widget is not visible (tabs keep pages alive on request).
  • Respect reduced motion. Check MediaQuery.disableAnimationsOf(context) and show a static frame instead (accessibility).
  • Dispose controllers. Both packages rely on tickers.
  • Licensing. Check the license of any animation you download before shipping it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you compute a Lottie animation's duration from its header?

    Out-point minus in-point gives the frame count, and the frame rate converts frames to seconds.
  2. 2. What is the main advantage of a Rive state machine over a Lottie timeline?

    Inputs such as booleans and triggers let your code steer the artwork without coding the transitions.
  3. 3. What does a null result from findInput usually mean?

    Names must match exactly; guard against null before using the input.
  4. 4. How can you show one chosen frame of a Lottie file?

    The controller value maps to a frame, so assigning it stops at that point.

Summary

  • Lottie plays exported timeline animations; give it an AnimationController to drive or scrub it.
  • Rive adds state machines whose inputs you set from Dart; names must match the file, and the runtime API depends on the package version.
  • Keep files light, stop off-screen animations, honor reduced motion and dispose controllers.

Next: Platform Channels and Plugins