mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-11-08 00:49:25 +08:00
An oversight from our side. We've previously recommended setting `isActive` to false on the StateMachine or Animation. But this will not propagate down to nested artboards. Diffs= e64daefff feat: adds play and pause to artboard (#7078) 2828b7b01 propagate volume to nested artboards (#7067) 4a9947630 Stop audio in iOS when backgrounded. (#7055) Co-authored-by: Gordon <pggordonhayes@gmail.com>
55 lines
1.3 KiB
Dart
55 lines
1.3 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> {
|
|
Artboard? _artboard;
|
|
|
|
bool get isPlaying => _artboard?.isPlaying ?? true;
|
|
|
|
/// Toggles between play and pause on the artboard
|
|
void _togglePlay() {
|
|
if (isPlaying) {
|
|
_artboard?.pause();
|
|
} else {
|
|
_artboard?.play();
|
|
}
|
|
|
|
// We call set state to update the Play/Pause Icon. This isn't needed
|
|
// to update Rive.
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Animation Example'),
|
|
),
|
|
body: RiveAnimation.asset(
|
|
'assets/off_road_car.riv',
|
|
animations: const ["idle"],
|
|
fit: BoxFit.cover,
|
|
onInit: (artboard) {
|
|
_artboard = artboard;
|
|
},
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _togglePlay,
|
|
tooltip: isPlaying ? 'Pause' : 'Play',
|
|
child: Icon(
|
|
isPlaying ? Icons.pause : Icons.play_arrow,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|