Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load and play issues between states #177

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions flare_flutter/lib/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ abstract class Cache<T extends CacheAsset> {
}
return asset;
}

T getAssetIfAvailable(String filename) {
T asset = _assets[filename];
if (asset != null && asset.isAvailable) {
return asset;
}
return null;
}
}
27 changes: 23 additions & 4 deletions flare_flutter/lib/flare_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ class FlareActorRenderObject extends FlareRenderBox {
@override
bool get isPlaying =>
!_isPaused &&
((_controller?.isActive?.value ?? false) || _animationLayers.isNotEmpty);
(_controller?.isActive?.value ?? true) &&
_animationLayers.isNotEmpty;

void onControllerActiveChange() {
updatePlayState();
Expand Down Expand Up @@ -260,7 +261,9 @@ class FlareActorRenderObject extends FlareRenderBox {
}
// file will change, let's clear out old animations.
_animationLayers.clear();
load();
if (!loadImmediately()) {
load();
}
}

bool _instanceArtboard() {
Expand All @@ -281,13 +284,14 @@ class FlareActorRenderObject extends FlareRenderBox {
_color.blue / 255.0,
_color.opacity
]);
_artboard.advance(0.0);
updateBounds();

if (_controller != null) {
_controller.initialize(_artboard);
}
_updateAnimation(onlyWhenMissing: true);
advance(0.0);
updateBounds();

markNeedsPaint();
return true;
}
Expand All @@ -304,6 +308,21 @@ class FlareActorRenderObject extends FlareRenderBox {
_instanceArtboard();
}

@override
bool loadImmediately() {
if (_filename == null) {
return false;
}

_actor = loadFlareFromCacheImmediately(_filename);
if (_actor == null || _actor.artboard == null) {
return false;
}

_instanceArtboard();
return true;
}

FlareCompletedCallback get completed => _completedCallback;
set completed(FlareCompletedCallback value) {
if (_completedCallback != value) {
Expand Down
7 changes: 7 additions & 0 deletions flare_flutter/lib/flare_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ Future<FlareCacheAsset> cachedActor(AssetBundle bundle, String filename) async {
}
return cache.getAsset(filename);
}

FlareCacheAsset cachedActorIfAvailable(AssetBundle bundle, String filename) {
FlareCache cache = _cache[bundle];
if (cache != null) {
return cache.getAssetIfAvailable(filename);
}
}
25 changes: 22 additions & 3 deletions flare_flutter/lib/flare_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ abstract class FlareRenderBox extends RenderBox {
return;
}
_assetBundle = value;
if (_assetBundle != null) {
_load();
if (_assetBundle != null && attached) {
if (!loadImmediately()) {
_load();
}
}
}

Expand Down Expand Up @@ -112,7 +114,9 @@ abstract class FlareRenderBox extends RenderBox {
super.attach(owner);
updatePlayState();
if (_assets.isEmpty && assetBundle != null) {
_load();
if (!loadImmediately()) {
_load();
}
}
}

Expand Down Expand Up @@ -248,6 +252,7 @@ abstract class FlareRenderBox extends RenderBox {

/// Perform any loading logic necessary for this scene.
Future<void> load() async {}
bool loadImmediately() { return false;}

void _unload() {
for (final FlareCacheAsset asset in _assets) {
Expand All @@ -259,6 +264,20 @@ abstract class FlareRenderBox extends RenderBox {

void onUnload() {}

FlutterActor loadFlareFromCacheImmediately(String filename) {
if (assetBundle == null || filename == null) {
return null;
}
FlareCacheAsset asset = cachedActorIfAvailable(assetBundle, filename);

if (!attached || asset == null) {
return null;
}
_assets.add(asset);
asset.ref();
return asset.actor;
}

/// Load a flare file from cache
Future<FlutterActor> loadFlare(String filename) async {
if (assetBundle == null || filename == null) {
Expand Down