/// Demonstrates how to play and pause a looping animation import 'package:flutter/material.dart'; import 'package:rive/rive.dart'; class PlayPauseAnimation extends StatefulWidget { const PlayPauseAnimation({Key? key}) : super(key: key); @override State createState() => _PlayPauseAnimationState(); } class _PlayPauseAnimationState extends State { /// Controller for playback late RiveAnimationController _controller; /// Toggles between play and pause animation states void _togglePlay() => setState(() => _controller.isActive = !_controller.isActive); /// Tracks if the animation is playing by whether controller is running bool get isPlaying => _controller.isActive; @override void initState() { super.initState(); _controller = SimpleAnimation('idle'); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Animation Example'), ), body: RiveAnimation.asset( 'assets/off_road_car.riv', fit: BoxFit.cover, controllers: [_controller], // Update the play state when the widget's initialized onInit: (_) => setState(() {}), ), floatingActionButton: FloatingActionButton( onPressed: _togglePlay, tooltip: isPlaying ? 'Pause' : 'Play', child: Icon( isPlaying ? Icons.pause : Icons.play_arrow, ), ), ); } }