Adds one-shot animation controller

This commit is contained in:
matt Sullivan
2021-06-14 14:41:10 -07:00
parent 4fdbb2a056
commit dddb78b62f
6 changed files with 121 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:rive_example/play_one_shot_animation.dart';
import 'package:rive_example/play_pause_animation.dart';
import 'package:rive_example/example_state_machine.dart';
import 'package:rive_example/liquid_download.dart';
@ -50,6 +51,20 @@ class Home extends StatelessWidget {
const SizedBox(
height: 10,
),
ElevatedButton(
child: const Text('Play One-Shot Animation'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute<void>(
builder: (context) => const PlayOneShotAnimation(),
),
);
},
),
const SizedBox(
height: 10,
),
ElevatedButton(
child: const Text('Button State Machine'),
onPressed: () {

View File

@ -0,0 +1,52 @@
/// Demonstrates play a one-shot animation on demand
import 'package:flutter/material.dart';
import 'package:rive/rive.dart';
class PlayOneShotAnimation extends StatefulWidget {
const PlayOneShotAnimation({Key? key}) : super(key: key);
@override
_PlayOneShotAnimationState createState() => _PlayOneShotAnimationState();
}
class _PlayOneShotAnimationState extends State<PlayOneShotAnimation> {
/// Controller for playback
late RiveAnimationController _controller;
/// Is the animation currently playing?
bool _isPlaying = false;
@override
void initState() {
super.initState();
_controller = OnShotAnimation(
'bounce',
autoplay: false,
onStop: () => setState(() => _isPlaying = false),
onStart: () => setState(() => _isPlaying = true),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('One-Shot Example'),
),
body: Center(
child: RiveAnimation.network(
'https://cdn.rive.app/vehicles.riv',
animations: const ['idle', 'curves'],
controllers: [_controller],
),
),
floatingActionButton: FloatingActionButton(
// disable the button while playing the animation
onPressed: () => _isPlaying ? null : _controller.isActive = true,
tooltip: 'Play',
child: const Icon(Icons.arrow_upward),
),
);
}
}

View File

@ -1,3 +1,5 @@
/// Demonstrates how to play and pause a looping animation
import 'package:flutter/material.dart';
import 'package:rive/rive.dart';
@ -9,10 +11,10 @@ class PlayPauseAnimation extends StatefulWidget {
}
class _PlayPauseAnimationState extends State<PlayPauseAnimation> {
// Controller for playback
/// Controller for playback
late RiveAnimationController _controller;
// Toggles between play and pause animation states
/// Toggles between play and pause animation states
void _togglePlay() =>
setState(() => _controller.isActive = !_controller.isActive);