I have a flare loading animation which takes time to load. Is there a way to pre-cache the flutter animation?
final AssetProvider assetProvider = AssetFlare(bundle: rootBundle, name: 'assets/animations/loop.flr');
cachedActor(assetProvider);
Is this the code to cache the actor? Then How do I load the cached animation?
To have the ability to use flare in your Flutter project, you need to add proper library. This library is called flare_flutter and you can check it on the pub packages site. Here you’ll find also instructions on how to install and use it. Basically, all you need to do is: a) Add this to your pubspec.yaml file:
The solution is quite simple. We need to pre-load the image before it is rendered. If it is preloaded and cached, the rendering of image when needed may seem instantaneous. Okay let's see how can we implement Precaching in Flutter.
Create a new shape that covers the star. Select the star, then click clipping box, then select the new shape. Go into animation mode and animate the position of the star. Create another shape to serve as a clipping box for the star. Flare provides package that makes it dead simple to use our animations into an iOS or Android app with Flutter.
Flare provides package that makes it dead simple to use our animations into an iOS or Android app with Flutter. Run to flutter create your_project to get started. The end goal is to build a usable animated icon navigation menu like the demo below: Add flare_flutter to pubspec.yaml
From https://github.com/2d-inc/Flare-Flutter/issues/180#issuecomment-550584347
You can use FlareCacheBuilder
to help you preload flr files for certain sections of your app
code snippet
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: FlareCacheBuilder(
["assets/Filip.flr"],
builder: (BuildContext context, bool isWarm) {
return !isWarm
? Container(child: Text("Loading..."))
: FlareActor(
"assets/Filip.flr",
alignment: Alignment.center,
fit: BoxFit.contain,
animation: _animationName,
);
},
),
)
],
),
),
);
}
If you want a 'global' load to your .flr files the correct way is using cachedActor
just like you posted.
Let's say you previously executed:
final assetProvider = AssetFlare(bundle: rootBundle, name: 'assets/animations/loop.flr');
await cachedActor(assetProvider);
When you try to use the same .flr
in a FlareActor
it will already try to first load this asset from the same cache you populated before.
In this example the Flare team does exactly this to warmup some files. But notice that they are using an old sintaxe for cachedActor
.
You can see at the source code here and here, that when loading the asset it will try at first to load from the cache.
One last thing to finish, FlareCacheBuilder
uses the same cachedActor
method internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With