mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-11-08 08:58:27 +08:00
53 lines
1.4 KiB
Dart
53 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:rive/rive.dart';
|
|
|
|
/// Demonstrates playing a one-shot animation on demand
|
|
class PlayOneShotAnimation extends StatefulWidget {
|
|
const PlayOneShotAnimation({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<PlayOneShotAnimation> 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 = OneShotAnimation(
|
|
'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.asset(
|
|
'assets/vehicles.riv',
|
|
animations: const ['idle', 'curves'],
|
|
fit: BoxFit.cover,
|
|
controllers: [_controller],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
// disable the button while playing the animation
|
|
onPressed: () => _isPlaying ? null : _controller.isActive = true,
|
|
tooltip: 'Bounce',
|
|
child: const Icon(Icons.arrow_upward),
|
|
),
|
|
);
|
|
}
|
|
}
|