diff --git a/lib/src/runtime_artboard.dart b/lib/src/runtime_artboard.dart index d71fbe9..5c7b52d 100644 --- a/lib/src/runtime_artboard.dart +++ b/lib/src/runtime_artboard.dart @@ -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 get linearAnimations => + animations.whereType(); + + /// Returns an iterable of state machines in the artboard + Iterable get stateMachines => + animations.whereType(); +} + /// 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. diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index ea86c28..757b910 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -18,7 +18,8 @@ class RiveAnimation extends StatefulWidget { final String name; final _Source src; final String? artboard; - final String? animation; + final List animations; + final List 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 { /// Rive controller - late RiveAnimationController _controller; + final _controllers = []; /// Active artboard Artboard? _artboard; @@ -110,15 +113,33 @@ class _RiveAnimationState extends State { 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(); } diff --git a/test/artboard_test.dart b/test/artboard_test.dart index d8c4d28..6d2692d 100644 --- a/test/artboard_test.dart +++ b/test/artboard_test.dart @@ -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); + }); }