Flutter 图像预加载 [英] Flutter image preload

查看:104
本文介绍了Flutter 图像预加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在应用程序启动时以某种方式预加载图像?就像我的抽屉里有一个背景图像,但是当我第一次打开抽屉时,我可以看到图像闪烁,就像从资产中获取然后显示一样,一旦我第一次看到它就会给我带来糟糕的体验 其他抽屉的开口按预期运行,因为它已被缓存.我想在应用加载时预取它,所以没有这种效果.

Is it possible to preload somehow the image on the app start? Like I have an background image in my drawer but for the first time when I open the drawer I can see the image blinks like it is fetched from assets and then displayed and it gives bad experience to me once I see it for the first time other openings of the drawer are behaving as expected because it is cached. I would like to prefetch it on the app load so there is no such effect.

推荐答案

使用precacheImage 函数用于在构建抽屉之前开始加载图像.例如,在包含抽屉的小部件中:

Use the precacheImage function to start loading an image before your drawer is built. For example, in the widget that contains your drawer:

class MyWidgetState extends State<MyWidget> {

  @override
  void initState() {
    // adjust the provider based on the image type
    precacheImage(new AssetImage('...'));
    super.initState();
  }

}

这篇关于Flutter 图像预加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆