搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案