Skip to main content

Command Palette

Search for a command to run...

Flutter Tips: How to avoid blinking of images when they are loaded for the first time

Published
0 min read

The problem

In this small post I will show you how to avoid 'blinking images' when they are loaded into AssetImage widget for the first time.

In Flutter I use Hero Widget for cross widget animations. When the user navigates to another view I want widget to 'fly' into new scene with a smooth animation. But, as you can see in the gif below, it is not so smooth:

no_precache_small.gif

The issue is caused by spending too much time loading image from the assets. After the image is rendered at least once, it is cached and next renders do not take so much time. But how do we make sure that first render is smooth?

The Solution

Use precacheImage ! You can call it from didUpdateDependencies:

 @override
  void didChangeDependencies() {
    List<AssetImage> allImages =
    BackgroundImage.getRandomImageForType(ImageType.LANDING)
        .getAllAvailableImages();
    allImages.forEach((AssetImage image) {
      precacheImage(image, context);
    });
    super.didChangeDependencies();
  }

Now all renders of Hero Widget are smooth and do not blink or have instantaneous size changes:

preache_small.gif

More from this blog

Дике Поле: Слобода 2. Січневе Велике Оновлення

Привіт, компана! Ось і настав час Січневого оновлення для нашої гри Слобода 2! Це оновлення безкоштовне для всіх, хто придбав гру. Якщо ж ще не придбали, то можете це зробити по цих посиланнях: Apple App Store: https://apps.apple.com/ua/app/loca-dese...

Jan 23, 20232 min read243
Дике Поле: Слобода 2. Січневе Велике Оновлення
P

Personal blog of Dmytro Gladkyi

48 posts

Unity & Web Developer.