mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-07-06 08:26:42 +08:00
53 lines
1.4 KiB
Dart
53 lines
1.4 KiB
Dart
/// 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<PlayPauseAnimation> createState() => _PlayPauseAnimationState();
|
|
}
|
|
|
|
class _PlayPauseAnimationState extends State<PlayPauseAnimation> {
|
|
/// 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,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|