Last active
March 11, 2021 13:45
-
-
Save lecuseasar/78366c39c0e5f263964c2dfa21afd399 to your computer and use it in GitHub Desktop.
Continuously playing a Looping Animation https://pub.dev/packages/rive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/services.dart'; | |
import 'package:rive/rive.dart'; | |
import 'package:flutter/material.dart'; | |
class MyRiveWidget extends StatefulWidget { | |
final String riveFile; | |
final String animationName; | |
final BoxFit fit; | |
const MyRiveWidget({ | |
Key key, | |
@required this.riveFile, | |
@required this.animationName, | |
this.fit, | |
}) : super(key: key); | |
@override | |
MyRiveWidgetState createState() => MyRiveWidgetState(); | |
} | |
class MyRiveWidgetState extends State<MyRiveWidget> { | |
//bool get isPlaying => _controller?.isActive ?? false; | |
Artboard riveArtboard; | |
RiveAnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
rootBundle.load(widget.riveFile).then( | |
(data) async { | |
final file = RiveFile(); | |
// Load the RiveFile from the binary data. | |
if (file.import(data)) { | |
// The artboard is the root of the animation and gets drawn in the | |
// Rive widget. | |
final artboard = file.mainArtboard; | |
// Add a controller to play back a known animation on the main/default | |
// artboard.We store a reference to it so we can toggle playback. | |
artboard.addController(_controller = SimpleAnimation(widget.animationName)); | |
setState(() => riveArtboard = artboard); | |
} | |
}, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return riveArtboard == null | |
? const SizedBox() | |
: Rive( | |
fit: widget.fit ?? BoxFit.cover, | |
artboard: riveArtboard, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment