mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-27 18:28:18 +08:00
Adds one-shot animation controller
This commit is contained in:
@ -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: () {
|
||||
|
52
example/lib/play_one_shot_animation.dart
Normal file
52
example/lib/play_one_shot_animation.dart
Normal 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),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user