From b408692f797ae7db04e49d6928e52532ce1ed68c Mon Sep 17 00:00:00 2001 From: Luigi Rosso Date: Sun, 14 Feb 2021 10:08:15 -0800 Subject: [PATCH] Fix issue #28 and help #51 and #56. --- lib/src/controllers/simple_controller.dart | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/src/controllers/simple_controller.dart b/lib/src/controllers/simple_controller.dart index e146dd4..ec931e6 100644 --- a/lib/src/controllers/simple_controller.dart +++ b/lib/src/controllers/simple_controller.dart @@ -12,6 +12,7 @@ class SimpleAnimation extends RiveAnimationController { LinearAnimationInstance _instance; final String animationName; + bool _stopOnNextApply = false; // Controls the level of mix for the animation, clamped between 0 and 1 double _mix; @@ -36,9 +37,27 @@ class SimpleAnimation extends RiveAnimationController { @override void apply(RuntimeArtboard artboard, double elapsedSeconds) { - _instance.animation.apply(_instance.time, coreContext: artboard, mix: mix); - if (!_instance.advance(elapsedSeconds)) { + if (_stopOnNextApply) { isActive = false; } + + // We apply before advancing. So we want to stop rendering only once the + // last advanced frame has been applied. This means tracking when the last + // frame is advanced, ensuring the next apply happens, and then finally + // stopping playback. We do this by tracking _stopOnNextApply making sure to + // reset it when the controller is re-activated. Fixes #28 and should help + // with issues #51 and #56. + _instance.animation.apply(_instance.time, coreContext: artboard, mix: mix); + if (!_instance.advance(elapsedSeconds)) { + _stopOnNextApply = true; + } + } + + @override + void onActivate() { + // We override onActivate to reset stopOnNextApply. This ensures that when + // the controller is re-activated after stopping, it doesn't prematurely + // stop itself. + _stopOnNextApply = false; } }