mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-07-13 21:57:32 +08:00
Adds animation and machine selection in RiveAnimation; getters on artboards for linaer animations and state machines
This commit is contained in:
@ -1,9 +1,21 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:rive/rive.dart';
|
||||||
import 'package:rive/src/rive_core/artboard.dart';
|
import 'package:rive/src/rive_core/artboard.dart';
|
||||||
import 'package:rive/src/rive_core/component.dart';
|
import 'package:rive/src/rive_core/component.dart';
|
||||||
import 'package:rive/src/rive_core/event.dart';
|
import 'package:rive/src/rive_core/event.dart';
|
||||||
import 'package:rive/src/core/core.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
|
/// 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
|
/// directly referenced. Use the Artboard type for any direct interactions with
|
||||||
/// an artboard, and use extension methods to add functionality to Artboard.
|
/// an artboard, and use extension methods to add functionality to Artboard.
|
||||||
|
@ -18,7 +18,8 @@ class RiveAnimation extends StatefulWidget {
|
|||||||
final String name;
|
final String name;
|
||||||
final _Source src;
|
final _Source src;
|
||||||
final String? artboard;
|
final String? artboard;
|
||||||
final String? animation;
|
final List<String> animations;
|
||||||
|
final List<String> stateMachines;
|
||||||
final BoxFit? fit;
|
final BoxFit? fit;
|
||||||
final Alignment? alignment;
|
final Alignment? alignment;
|
||||||
|
|
||||||
@ -26,7 +27,8 @@ class RiveAnimation extends StatefulWidget {
|
|||||||
const RiveAnimation.asset(
|
const RiveAnimation.asset(
|
||||||
this.name, {
|
this.name, {
|
||||||
this.artboard,
|
this.artboard,
|
||||||
this.animation,
|
this.animations = const [],
|
||||||
|
this.stateMachines = const [],
|
||||||
this.fit,
|
this.fit,
|
||||||
this.alignment,
|
this.alignment,
|
||||||
}) : src = _Source.asset;
|
}) : src = _Source.asset;
|
||||||
@ -34,7 +36,8 @@ class RiveAnimation extends StatefulWidget {
|
|||||||
const RiveAnimation.network(
|
const RiveAnimation.network(
|
||||||
this.name, {
|
this.name, {
|
||||||
this.artboard,
|
this.artboard,
|
||||||
this.animation,
|
this.animations = const [],
|
||||||
|
this.stateMachines = const [],
|
||||||
this.fit,
|
this.fit,
|
||||||
this.alignment,
|
this.alignment,
|
||||||
}) : src = _Source.network;
|
}) : src = _Source.network;
|
||||||
@ -45,7 +48,7 @@ class RiveAnimation extends StatefulWidget {
|
|||||||
|
|
||||||
class _RiveAnimationState extends State<RiveAnimation> {
|
class _RiveAnimationState extends State<RiveAnimation> {
|
||||||
/// Rive controller
|
/// Rive controller
|
||||||
late RiveAnimationController _controller;
|
final _controllers = <RiveAnimationController>[];
|
||||||
|
|
||||||
/// Active artboard
|
/// Active artboard
|
||||||
Artboard? _artboard;
|
Artboard? _artboard;
|
||||||
@ -110,15 +113,33 @@ class _RiveAnimationState extends State<RiveAnimation> {
|
|||||||
throw FormatException('No animations in artboard ${artboard.name}');
|
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);
|
setState(() => _artboard = artboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_controller.dispose();
|
_controllers.forEach((c) => c.dispose());
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,4 +33,10 @@ void main() {
|
|||||||
expect(artboard.animationByName('Animation 1') is LinearAnimationInstance,
|
expect(artboard.animationByName('Animation 1') is LinearAnimationInstance,
|
||||||
true);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user