Adds animation and machine selection in RiveAnimation; getters on artboards for linaer animations and state machines

This commit is contained in:
matt Sullivan
2021-06-10 12:51:52 -07:00
parent a22735177f
commit 05ceb34fa1
3 changed files with 46 additions and 7 deletions

View File

@ -1,9 +1,21 @@
import 'package:flutter/foundation.dart';
import 'package:rive/rive.dart';
import 'package:rive/src/rive_core/artboard.dart';
import 'package:rive/src/rive_core/component.dart';
import 'package:rive/src/rive_core/event.dart';
import 'package:rive/src/core/core.dart';
/// Adds getters for linear animations and state machines
extension RuntimeArtboardGetters on RuntimeArtboard {
/// Returns an iterable of linear animations in the artboard
Iterable<LinearAnimation> get linearAnimations =>
animations.whereType<LinearAnimation>();
/// Returns an iterable of state machines in the artboard
Iterable<StateMachine> get stateMachines =>
animations.whereType<StateMachine>();
}
/// This artboard type is purely for use by the runtime system and should not be
/// directly referenced. Use the Artboard type for any direct interactions with
/// an artboard, and use extension methods to add functionality to Artboard.

View File

@ -18,7 +18,8 @@ class RiveAnimation extends StatefulWidget {
final String name;
final _Source src;
final String? artboard;
final String? animation;
final List<String> animations;
final List<String> stateMachines;
final BoxFit? fit;
final Alignment? alignment;
@ -26,7 +27,8 @@ class RiveAnimation extends StatefulWidget {
const RiveAnimation.asset(
this.name, {
this.artboard,
this.animation,
this.animations = const [],
this.stateMachines = const [],
this.fit,
this.alignment,
}) : src = _Source.asset;
@ -34,7 +36,8 @@ class RiveAnimation extends StatefulWidget {
const RiveAnimation.network(
this.name, {
this.artboard,
this.animation,
this.animations = const [],
this.stateMachines = const [],
this.fit,
this.alignment,
}) : src = _Source.network;
@ -45,7 +48,7 @@ class RiveAnimation extends StatefulWidget {
class _RiveAnimationState extends State<RiveAnimation> {
/// Rive controller
late RiveAnimationController _controller;
final _controllers = <RiveAnimationController>[];
/// Active artboard
Artboard? _artboard;
@ -110,15 +113,33 @@ class _RiveAnimationState extends State<RiveAnimation> {
throw FormatException('No animations in artboard ${artboard.name}');
}
final animationName = widget.animation ?? artboard.animations.first.name;
// Create animations
// If there are no animations or state machines specified, select a default
// animation
final animationNames =
widget.animations.isEmpty && widget.stateMachines.isEmpty
? [artboard.animations.first.name]
: widget.animations;
animationNames.forEach((name) => artboard
.addController((_controllers..add(SimpleAnimation(name))).last));
// Create state machines
final stateMachineNames = widget.stateMachines;
stateMachineNames.forEach((name) {
final controller = StateMachineController.fromArtboard(artboard, name);
if (controller != null) {
artboard.addController((_controllers..add(controller)).last);
}
});
artboard.addController(_controller = SimpleAnimation(animationName));
setState(() => _artboard = artboard);
}
@override
void dispose() {
_controller.dispose();
_controllers.forEach((c) => c.dispose());
super.dispose();
}

View File

@ -33,4 +33,10 @@ void main() {
expect(artboard.animationByName('Animation 1') is LinearAnimationInstance,
true);
});
test('Linear animations can be retreived ffrom artboards', () {
final artboard = riveFile.mainArtboard;
expect(artboard.linearAnimations.toList().length, 2);
expect(artboard.stateMachines.toList().length, 1);
});
}