Skip to content

Commit

Permalink
Prevent coldLoad (async) from happening when widget isn’t ready to load.
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-rosso committed Oct 28, 2019
1 parent 506837e commit 43716dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 11 additions & 3 deletions flare_flutter/lib/flare_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,26 @@ class FlareActorRenderObject extends FlareRenderBox {
_color.blue / 255.0,
_color.opacity
]);
_artboard.advance(0.0);
updateBounds();

if (_controller != null) {
_controller.initialize(_artboard);
}
_updateAnimation(onlyWhenMissing: true);
// Immediately update the newly instanced artboard and compute
// bounds so that the widget can take up the necessary space
advance(0.0);
updateBounds();

markNeedsPaint();
return true;
}

/// Attempt a warm load, this is the optimal case when the
@override
bool get canLoad {
return super.canLoad && _filename != null;
}

/// Attempt a warm load, thfis is the optimal case when the
/// required asset is already in the cache.
@override
bool warmLoad() {
Expand Down
16 changes: 14 additions & 2 deletions flare_flutter/lib/flare_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class FlareRenderBox extends RenderBox {
return;
}
_assetBundle = value;
if (_assetBundle != null) {
if (_assetBundle != null && attached) {
load();
}
}
Expand Down Expand Up @@ -241,6 +241,15 @@ abstract class FlareRenderBox extends RenderBox {
return false;
}

/// Prevent loading when the renderbox isn't attached or the asset bundle
/// is yet to be set. This prevents unneccesarily hitting an async path
/// during load. A warmLoad would fail which then falls back to a coldLoad.
/// Due to the async nature, any further sync calls would be blocked as we
/// gate load with _isLoading.
bool get canLoad {
return attached && _assetBundle != null;
}

Future<void> coldLoad() async {}

/// Trigger the loading process. This will attempt a sync warm load,
Expand All @@ -249,6 +258,9 @@ abstract class FlareRenderBox extends RenderBox {
/// draw any empty frames.
///
void load() {
if (!canLoad) {
return;
}
if (_isLoading) {
_reloadQueued = true;
return;
Expand Down Expand Up @@ -284,7 +296,7 @@ abstract class FlareRenderBox extends RenderBox {
}

void onUnload() {}

/// Load a flare file from cache
FlutterActor getWarmFlare(String filename) {
if (assetBundle == null || filename == null) {
Expand Down

0 comments on commit 43716dd

Please sign in to comment.