搜索插件、代码仓与 APP 分类

插件详情v4.0.6

cached_network_image

用于加载和缓存网络图片的 Flutter 库。也可与占位符和错误组件一起使用。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
366万
喜欢数
6990
Pub 分数
150

安装插件

flutter pub add cached_network_image

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个用于高效加载和缓存网络图片的 Flutter 包,支持占位符和错误组件。通过本地存储图片,减少重复下载,提升应用性能并降低带宽使用。适用于从远程源显示大量图片的应用程序,例如社交媒体动态或产品目录。该库异步处理图片加载,并提供可自定义的加载状态和失败时的 UI。

适合场景

  • 在应用程序中显示动态图像内容
  • 从远程服务器高效加载图像
  • 加载时显示占位符图像
  • 优雅地处理图像加载失败
  • 通过本地缓存减少数据使用
  • 构建图像密集型用户界面

主要特点

  • 自动图像缓存
  • 可自定义的占位符和错误小部件
  • 支持所有主要平台
  • 高效的内存和磁盘管理
  • 异步图像加载
  • 可配置的缓存大小和持续时间

适合对象

  • 频繁获取图像的应用
  • 注重性能和用户体验的开发者
  • 需要离线图像访问的项目
  • 社交媒体和电子商务应用
  • 针对多个平台的应用程序

使用建议

  • 需要适当的缓存管理以避免存储问题
  • 可能需要针对大型图像集进行调整
  • 平台特定的行为可能会略有不同
  • 初始设置需要了解缓存策略
  • 不太适合非常小的静态图像集合

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Cached network image

pub package codecov Build Status

A flutter library to show images from the internet and keep them in the cache directory.

Sponsors


Try the Flutter Chat Tutorial 💬

How to use

The CachedNetworkImage can be used directly or through the ImageProvider. Both the CachedNetworkImage as CachedNetworkImageProvider have minimal support for web. It currently doesn't include caching.

With a placeholder:

CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
     ),

Or with a progress indicator:

CachedNetworkImage(
       imageUrl: "http://via.placeholder.com/350x150",
       progressIndicatorBuilder: (context, url, downloadProgress) => 
               CircularProgressIndicator(value: downloadProgress.progress),
       errorWidget: (context, url, error) => Icon(Icons.error),
    ),
Image(image: CachedNetworkImageProvider(url))

When you want to have both the placeholder functionality and want to get the imageprovider to use in another widget you can provide an imageBuilder:

CachedNetworkImage(
  imageUrl: "http://via.placeholder.com/200x150",
  imageBuilder: (context, imageProvider) => Container(
    decoration: BoxDecoration(
      image: DecorationImage(
          image: imageProvider,
          fit: BoxFit.cover,
          colorFilter:
              ColorFilter.mode(Colors.red, BlendMode.colorBurn)),
    ),
  ),
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
),

How it works

The cached network images stores and retrieves files using the flutter_cache_manager.

FAQ

My app crashes when the image loading failed. (I know, this is not really a question.)

Does it really crash though? The debugger might pause, as the Dart VM doesn't recognize it as a caught exception; the console might print errors; even your crash reporting tool might report it (I know, that really sucks). However, does it really crash? Probably everything is just running fine. If you really get an app crashes you are fine to report an issue, but do that with a small example so we can reproduce that crash.

See for example this or this answer on previous posted issues.

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目