diff --git a/flare_dart/CHANGELOG.md b/flare_dart/CHANGELOG.md index deea0f1..e3ff9ea 100644 --- a/flare_dart/CHANGELOG.md +++ b/flare_dart/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.4.4] - 2019-07-24 11:43:51 + +Adding getArtboard method to Actor class. Allows finding artboards by name. + ## [1.4.3] - 2019-07-06 11:08:53 Some cleanup done while fixing issue #104 (using intrinsic artboard size as an option). diff --git a/flare_dart/lib/actor.dart b/flare_dart/lib/actor.dart index e854667..8d7acfd 100644 --- a/flare_dart/lib/actor.dart +++ b/flare_dart/lib/actor.dart @@ -22,6 +22,10 @@ abstract class Actor { Actor(); ActorArtboard get artboard => _artboards.isNotEmpty ? _artboards.first : null; + ActorArtboard getArtboard(String name) => name == null + ? artboard + : _artboards.firstWhere((artboard) => artboard?.name == name, + orElse: () => null); int get version { return _version; diff --git a/flare_flutter/CHANGELOG.md b/flare_flutter/CHANGELOG.md index 19fe297..036497e 100644 --- a/flare_flutter/CHANGELOG.md +++ b/flare_flutter/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.5.5] - 2019-07-24 11:44:36 + + - Adding artboard option to FlareActor. Use this to change which artboard gets displayed by the FlareActor widget. + - Fixed incorrect signature of load method. If you were deriving FlareRenderBox, you'll need to update it to match. It's a minor change from void to Future. + - Added some documentation to the FlareActor parameters. + ## [1.5.4] - 2019-07-08 21:10:50 - Using Uint16List for vertex indices now that Flutter Stable has been updated. diff --git a/flare_flutter/lib/flare_actor.dart b/flare_flutter/lib/flare_actor.dart index 818cb20..374804e 100644 --- a/flare_flutter/lib/flare_actor.dart +++ b/flare_flutter/lib/flare_actor.dart @@ -12,31 +12,67 @@ import 'flare_controller.dart'; typedef void FlareCompletedCallback(String name); class FlareActor extends LeafRenderObjectWidget { + /// Name of the Flare file to be loaded from the AssetBundle. final String filename; + + /// The name of the artboard to display. + final String artboard; + + /// The name of the animation to play. final String animation; + + /// When true, the animation will be applied at the end of its duration. final bool snapToEnd; + + /// The BoxFit strategy used to scale the Flare content into the + /// bounds of this widget. final BoxFit fit; + + /// The alignment that will be applied in conjuction to the [fit] to align + /// the Flare content within the bounds of this widget. final Alignment alignment; + + /// When true, animations do not advance. final bool isPaused; + + /// When true, the Flare content will be clipped against the bounds of this + /// widget. final bool shouldClip; + + /// The [FlareController] used to drive animations/mixing/procedural hierarchy + /// manipulation of the Flare contents. final FlareController controller; + + /// Callback invoked when [animation] has completed. If [animation] is looping + /// this callback is never invoked. final FlareCompletedCallback callback; + + /// The color to override any fills/strokes with. final Color color; + + /// The name of the node to use to determine the bounds of the content. + /// When null it will default to the bounds of the artboard. final String boundsNode; + + /// When true the intrinsic size of the artboard will be used as the + /// dimensions of this widget. final bool sizeFromArtboard; - const FlareActor(this.filename, - {this.boundsNode, - this.animation, - this.fit = BoxFit.contain, - this.alignment = Alignment.center, - this.isPaused = false, - this.snapToEnd = false, - this.controller, - this.callback, - this.color, - this.shouldClip = true, - this.sizeFromArtboard = false}); + const FlareActor( + this.filename, { + this.boundsNode, + this.animation, + this.fit = BoxFit.contain, + this.alignment = Alignment.center, + this.isPaused = false, + this.snapToEnd = false, + this.controller, + this.callback, + this.color, + this.shouldClip = true, + this.sizeFromArtboard = false, + this.artboard, + }); @override RenderObject createRenderObject(BuildContext context) { @@ -53,7 +89,8 @@ class FlareActor extends LeafRenderObjectWidget { ..color = color ..shouldClip = shouldClip ..boundsNodeName = boundsNode - ..useIntrinsicSize = sizeFromArtboard; + ..useIntrinsicSize = sizeFromArtboard + ..artboardName = artboard; } @override @@ -70,7 +107,8 @@ class FlareActor extends LeafRenderObjectWidget { ..color = color ..shouldClip = shouldClip ..boundsNodeName = boundsNode - ..useIntrinsicSize = sizeFromArtboard; + ..useIntrinsicSize = sizeFromArtboard + ..artboardName = artboard; } @override @@ -94,12 +132,24 @@ class FlareAnimationLayer { class FlareActorRenderObject extends FlareRenderBox { Mat2D _lastControllerViewTransform; String _filename; + String _artboardName; String _animationName; String _boundsNodeName; FlareController _controller; FlareCompletedCallback _completedCallback; bool snapToEnd = false; bool _isPaused = false; + FlutterActor _actor; + + String get artboardName => _artboardName; + set artboardName(String name) { + if (_artboardName == name) { + return; + } + _artboardName = name; + _instanceArtboard(); + } + bool get isPaused => _isPaused; set isPaused(bool value) { if (_isPaused == value) { @@ -213,38 +263,45 @@ class FlareActorRenderObject extends FlareRenderBox { load(); } + bool _instanceArtboard() { + if (_actor == null || _actor.artboard == null) { + return false; + } + FlutterActorArtboard artboard = _actor + .getArtboard(_artboardName) + .makeInstance() as FlutterActorArtboard; + artboard.initializeGraphics(); + _artboard = artboard; + intrinsicSize = Size(artboard.width, artboard.height); + _artboard.overrideColor = _color == null + ? null + : Float32List.fromList([ + _color.red / 255.0, + _color.green / 255.0, + _color.blue / 255.0, + _color.opacity + ]); + _artboard.advance(0.0); + updateBounds(); + + if (_controller != null) { + _controller.initialize(_artboard); + } + _updateAnimation(onlyWhenMissing: true); + markNeedsPaint(); + return true; + } + @override - void load() { + Future load() async { if (_filename == null) { return; } - super.load(); - loadFlare(_filename).then((FlutterActor actor) { - if (actor == null || actor.artboard == null) { - return; - } - FlutterActorArtboard artboard = - actor.artboard.makeInstance() as FlutterActorArtboard; - artboard.initializeGraphics(); - _artboard = artboard; - intrinsicSize = Size(artboard.width, artboard.height); - _artboard.overrideColor = _color == null - ? null - : Float32List.fromList([ - _color.red / 255.0, - _color.green / 255.0, - _color.blue / 255.0, - _color.opacity - ]); - _artboard.advance(0.0); - updateBounds(); - - if (_controller != null) { - _controller.initialize(_artboard); - } - _updateAnimation(onlyWhenMissing: true); - markNeedsPaint(); - }); + _actor = await loadFlare(_filename); + if (_actor == null || _actor.artboard == null) { + return; + } + _instanceArtboard(); } FlareCompletedCallback get completed => _completedCallback; diff --git a/flare_flutter/lib/flare_render_box.dart b/flare_flutter/lib/flare_render_box.dart index 9b5879f..46be707 100644 --- a/flare_flutter/lib/flare_render_box.dart +++ b/flare_flutter/lib/flare_render_box.dart @@ -236,7 +236,7 @@ abstract class FlareRenderBox extends RenderBox { bool _isLoading = false; bool get isLoading => _isLoading; - void _load() async { + Future _load() async { if (_isLoading) { return; } @@ -247,7 +247,7 @@ abstract class FlareRenderBox extends RenderBox { } /// Perform any loading logic necessary for this scene. - void load() async {} + Future load() async {} void _unload() { for (final FlareCacheAsset asset in _assets) {