android - Flutter 图像缓存:图像正在重新加载
问题描述
我正在制作一个墙纸应用程序,它显示来自 GridView.builder 列表中的许多高清图像(大约 90 张高清图像),并且这些图像没有被缓存。每次我上下滚动时它们都会重新加载。我尝试使用正常的网络图像甚至 CachedNetworkImage 但结果相同。有什么解决办法吗?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Fetch Data JSON"),),
body: isLoading? Container(
alignment: new Alignment(0, 0),
child: CircularProgressIndicator(),
decoration: new BoxDecoration(color: Color.fromARGB(230, 0, 0, 0)),)
:
Container(
decoration: new BoxDecoration(color: Color.fromARGB(230, 0, 0, 0)),
child: new GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1,childAspectRatio:3),
itemCount: list.length,
padding: EdgeInsets.all(0.0),
itemBuilder: (BuildContext context, int index) {
final cacheimg = CachedNetworkImage(
imageUrl: list[index].imgcollectionsrcimage.toString(),
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error, color: Colors.white,),
);
return Stack(
children: <Widget>[
//new Image.network(list[index].imgcollectionsrcimage.toString())
/*
new CachedNetworkImage(
imageUrl: imgsrc1[index],
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error, color: Colors.white,),
),
*/
////////////////////////////////////////////srcimage/////////////////////////////////////////////////
new GestureDetector(
//onTap: (){_gotoImageCollections(list[index].hreflink.toString());},
child: new Container (
child: Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(Radius.circular(5)),
image: DecorationImage(
//colorFilter: const ColorFilter.mode(const Color.fromARGB(150, 0, 0, 0), BlendMode.darken),
image: NetworkImage(list[index].imgcollectionsrcimage.toString()),
fit: BoxFit.cover,
),
boxShadow: <BoxShadow>[new BoxShadow(color: const Color.fromARGB(150, 0, 0, 0),offset: new Offset(0.0, 3.0),blurRadius: 5.0)]
),
),
padding: EdgeInsets.all(2.0),
//decoration: BoxDecoration(boxShadow:<BoxShadow>[new BoxShadow(color: const Color.fromARGB(50, 0, 0, 0),offset: new Offset(0.0, 0.0),blurRadius: 0.0)]),
),
),
],
);
},
),
)
);
}
}
解决方案
您可以使用以下内容自定义图像缓存的最大空间。
class CustomImageCache extends WidgetsFlutterBinding {
@override
ImageCache createImageCache() {
ImageCache imageCache = super.createImageCache();
// Set your image cache size
imageCache.maximumSizeBytes = 1024 * 1024 * 100; // 100 MB
return imageCache;
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (kReleaseMode) {
CustomImageCache();
}
runApp(MyApp());
}
这是为了覆盖 Flutter 的内部图像缓存,而不是 CacheNetworkImage 之一。Flutter 默认使用 100 MiB,因此您可以尝试更高的值。
推荐阅读
- python-3.x - 从 fashion_MNIST 数据打印图像会给出彩色图像?
- r - 在 R 中格式化数字并转换为具有特定字体和大小的文本
- django - Django rest-auth 自定义登录表单 - 无法识别输入的电子邮件:“必须包括 \"email\" 和 \"password\"。”
- android - Android 8.1 上的 ADB wifi 永久
- docker - 如何使用 docker-compose 在 Openshift Container Platform 中部署?
- design-patterns - 复合设计模式 - 如何创建计算器
- django - 在 Django 中实现 PostgreSQL HStore
- php - 如何更改php中的打印变量?
- linux - 循环遍历 CSV 文件中的每一列并将不同的值导出到文件中
- javascript - 如何在 webdriverIO 的屏幕截图名称中获取测试套件/测试用例名称?