如何在Flutter中更改或替换ImageCache? [英] How do I change or replace the ImageCache in Flutter?

查看:228
本文介绍了如何在Flutter中更改或替换ImageCache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flutter应用中更改ImageCache的行为。例如,我想尝试不同的驱逐策略。或者,我只是想要零缓存(用于实验)。

I want to change the behavior of the ImageCache in my Flutter app. For example, I want to experiment with different eviction strategies. Or, I simply want zero caching (for experimenting).

如何替换以更改ImageCache?

How do I replace to change the ImageCache?

推荐答案

创建一个从WidgetsFlutterBinding继承的类。
在该类上重写createImageCache以返回您选择的ImageCache实现。
在调用runApp之前先调用该类的构造函数。

Create a class that inherits from WidgetsFlutterBinding. Override createImageCache on that class to return the ImageCache implementation of your choice. Call the constructor on that class before you call runApp.

每个应用程序都有一个绑定。创建的第一个绑定(BindingBase的子类)将成为该绑定。调用runApp()会实例化WidgetsFlutterBinding绑定,这通常是您想要的。 WidgetsFlutterBinding混入的类之一,PaintingBinding,引入了createImageCache作为一种使您覆盖图像缓存的方法。它调用该方法,并将全局imageCache设置为该方法返回的值。

There's one binding per application. The first binding (subclass of BindingBase) that is created gets to be that binding. Calling runApp() instantiates the WidgetsFlutterBinding binding, which is usually what you want. One of the classes that WidgetsFlutterBinding mixes in, the PaintingBinding, introduces createImageCache as a way to let you override the image cache. It calls that method, and sets the global imageCache to whatever that method returns.

这篇关于如何在Flutter中更改或替换ImageCache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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