搜尋分類、套件與程式碼儲存庫

octo_image 封面
套件詳情v2.1.3

octo_image

一個多功能的 Flutter 圖像組件。支援佔位符、錯誤組件和帶漸變效果的圖像轉換器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
415萬
喜歡數
767
Pub 分數
160

安裝套件

flutter pub add octo_image

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Octo Image 是一個多功能的 Flutter 小部件,專為處理影像載入而設計,具備進階功能如佔位符、錯誤處理以及平滑的淡入淡出過渡。它支援影像變換,並提供乾淨且可自訂的介面,可在多個平台顯示影像。此套件以效能與開發者便利性為目標,簡化了 Flutter 應用中常見的影像顯示挑戰。該套件可無縫整合至現有的影像載入工作流程中,並在載入狀態期間透過視覺反饋提升使用者體驗。

適用情境

  • 顯示網路圖片並提供備用方案
  • 實現加載佔位符
  • 優雅地處理圖像加載錯誤
  • 應用圖像濾鏡或變換
  • 創建平滑的淡入效果
  • 透過預加載提升UI回應速度

主要特色

  • 支援佔位符和錯誤小部件
  • 內建的影像轉換功能
  • 平滑淡入淡出過渡
  • 跨平台相容性
  • 可自定義的加載和錯誤狀態
  • 高效緩存和記憶體管理

適合對象

  • 需要強大圖像加載功能的應用程式
  • 尋求增強圖像使用者體驗的開發者
  • 具有動態或遠端圖像來源的專案
  • UI密集型應用程式,頻繁更新影像
  • 優先考慮跨平台一致影像行為的團隊

使用建議

  • 需要額外設定才能進行高級轉換
  • 可能會略微增加捆綁大小
  • 僅限圖像特定用途
  • 取決於底層的影像載入函式庫
  • 不適合簡單的靜態圖像顯示

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

OctoImage

pub package Build Status codecov

An image library for showing placeholders, error widgets and transform your image.

Recommended using with CachedNetworkImage version 2.2.0 or newer.

Getting Started

The OctoImage widget needs an ImageProvider to show the image. You can either supply the widget with a placeholder or progress indicator, an ImageBuilder and/or an error widget.

However, what OctoImage makes is the use of OctoSets. OctoSets are predefined combinations of placeholders, imagebuilders and error widgets.

So, either set the all the components yourself:

OctoImage(
  image: CachedNetworkImageProvider(
      'https://blurha.sh/assets/images/img1.jpg'),
  placeholderBuilder: OctoPlaceholder.blurHash(
    'LEHV6nWB2yk8pyo0adR*.7kCMdnj',
  ),
  errorBuilder: OctoError.icon(color: Colors.red),
  fit: BoxFit.cover,
);

Or use an OctoSet:

OctoImage.fromSet(
  fit: BoxFit.cover,
  image: CachedNetworkImageProvider(
    'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Macaca_nigra_self-portrait_large.jpg/1024px-Macaca_nigra_self-portrait_large.jpg',
  ),
  octoSet: OctoSet.circleAvatar(
    backgroundColor: Colors.red,
    text: Text("M"),
  ),
);

The CircleAvatar set shows a colored circle with the text inside during loading and when the image failed loading. When the image loads it animates to the image clipped as a circle.

ImageProviders

The recommended one is CachedNetworkImageProvider, as that supports the progress indicator, error and caching. It also works on Android, iOS, web and macOS, although without caching on the web. Make sure you use at least version 2.2.0.

The second best is NetworkImage, but any ImageProvider works in theory. However, for some ImageProviders (such as MemoryImage) it doesn't make sense to use OctoImage.

Placeholders and progress indicators

It would be best if you used either a placeholder or a progress indicator, but not both. Placeholders are only building once the image starts loading, but progress indicators are rebuilt every time new progress information is received. So if you don't use that progress indication, for example, with a static image, you should use a placeholder.

The most simple progress indicators use a CircularProgressIndicator.

OctoImage(
  image: image,
  progressIndicatorBuilder: (context) => 
    const CircularProgressIndicator(),
),
OctoImage(
  image: image,
  progressIndicatorBuilder: (context, progress) {
    double value;
    if (progress != null && progress.expectedTotalBytes != null) {
      value =
          progress.cumulativeBytesLoaded / progress.expectedTotalBytes;
    }
    return CircularProgressIndicator(value: value);
  },
),

However, because these are used so often, we prebuild these widgets for you. Just use OctoProgressIndicator.circularProgressIndicator()

OctoImage(
  image: image,
  progressIndicatorBuilder: OctoProgressIndicator.circularProgressIndicator(),
),

All included placeholders and progress indicators:

OctoPlaceholder Explanation
blurHash Shows a BlurHash image
circleAvatar Shows a colored circle with a text
circularProgressIndicator Shows a circularProgressIndicator with indeterminate progress.
frame Shows the Flutter Placeholder
OctoProgressIndicator Explanation
circularProgressIndicator Shows a simple CircularProgressIndicator

Error widgets

Error widgets are shown when the ImageProvider throws an error because the image failed loading. You can build a custom widget, or use the prebuild widgets:

OctoImage(
  image: image,
  errorBuilder: (context, error, stacktrace) =>
    const Icon(Icons.error),
);
OctoImage(
  image: image,
  errorBuilder: OctoError.icon(),
),

All included error widgets are:

OctoError Explanation
blurHash Shows a BlurHash placeholder with an error icon.
circleAvatar Shows a colored circle with a text
icon Shows an icon, default to Icons.error
placeholderWithErrorIcon Shows any placeholder with an icon op top.

Image builders

Image builders can be used to adapt the image before it is shown. For example the circleAvatar clips the image in a circle, but you could also add an overlay or anything else.

The builder function supplies a context and a child. The child is the image widget that is rendered.

An example that shows the image with 50% opacity:

OctoImage(
  image: image,
  imageBuilder: (context, child) => Opacity(
    opacity: 0.5,
    child: child,
  ),
),

A prebuild image transformer that clips the image as a circle:

OctoImage(
  image: image,
  imageBuilder: OctoImageTransformer.circleAvatar(),
),

All included image transformers are:

OctoImageTransformer Explanation
circleAvatar Clips the image in a circle

OctoSets

You get the most out of OctoImage when you use OctoSets. These sets contain a combination of a placeholder or progress indicator, an image builder and/or an error widget builder. It always contains at least a placeholder or progress indicator and an error widget.

You can use them with OctoImage.fromSet:

OctoImage.fromSet(
  image: image,
  octoSet: OctoSet.circleAvatar(backgroundColor: Colors.red, text: Text("M")),
),

All included OctoSets are:

OctoSet Explanation
circleAvatar Shows a colored circle with text during load and error. Clips the image after successful load.
circularIndicatorAndIcon Shows a circularProgressIndicator with or without progress and an icon on error.
Blurhash

You can easily create your own OctoSet though, for example if you want to use blurHash as a placeholder:

/// Simple set to show [OctoPlaceholder.circularProgressIndicator] as
/// placeholder and [OctoError.icon] as error.
OctoSet blurHash(
  String hash, {
  BoxFit? fit,
  Text? errorMessage,
}) {
  return OctoSet(
    placeholderBuilder: blurHashPlaceholderBuilder(hash, fit: fit),
    errorBuilder: blurHashErrorBuilder(hash, fit: fit),
  );
}

OctoPlaceholderBuilder blurHashPlaceholderBuilder(String hash, {BoxFit? fit}) {
  return (context) => SizedBox.expand(
    child: Image(
      image: BlurHashImage(hash),
      fit: fit ?? BoxFit.cover,
    ),
  );
}


OctoErrorBuilder blurHashErrorBuilder(
  String hash, {
  BoxFit? fit,
  Text? message,
  IconData? icon,
  Color? iconColor,
  double? iconSize,
}) {
  return OctoError.placeholderWithErrorIcon(
    blurHashPlaceholderBuilder(hash, fit: fit),
    message: message,
    icon: icon,
    iconColor: iconColor,
    iconSize: iconSize,
  );
}

Contribute

If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our contribution guide and send us your pull request.

PR's with any new prebuild widgets or sets are highly appreciated.

Support

  • Feel free to open an issue. Make sure to use one of the templates!
  • Commercial support is available. Integration with your app or services, samples, feature request, etc. Email: [hello@baseflow.com](mailto:hello@baseflow.com)
  • Powered by: baseflow.com

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單