Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pre-cache flutter flare animation

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?

like image 266
ilovecse Avatar asked Feb 09 '20 06:02

ilovecse


People also ask

How to add flare to your flutter project?

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:

How to implement precaching in flutter?

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.

How to animate a star 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.

How to create an animated navigation menu 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


2 Answers

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,
                        );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
like image 155
chunhunghan Avatar answered Oct 02 '22 13:10

chunhunghan


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.

like image 30
Julio Henrique Bitencourt Avatar answered Oct 02 '22 12:10

Julio Henrique Bitencourt