flutter_cached_network_image

repository·develop·Indexed 25 days ago

https://github.com/baseflow/flutter_cached_network_image

A Flutter library for displaying network images with automatic local caching to improve performance and reduce data usage. It provides the CachedNetworkImage widget for high-level usage with placeholders, progress indicators, and error widgets, as well as CachedNetworkImageProvider for use in standard Image widgets. The library utilizes flutter_cache_manager for storage and offers features like memory/disk cache resizing, custom HTTP headers, and manual cache eviction.

Tokens
2.1K
Snippets
6
Records
17
Agent score
81%

What's inside flutter_cached_network_image

  1. Customize iOS launch screen assets

    develop

    To customize the launch screen for the iOS version of your Flutter app, you can replace the image files located in the LaunchImage.imageset directory with your own assets.

    Alternatively, you can manage these assets using Xcode:

    1. Open your Flutter project's iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  2. Use CachedNetworkImage with a progress indicator

    develop

    To show the actual download progress of an image, use the progressIndicatorBuilder. This provides a downloadProgress object containing the current progress value.

    ```dart
    CachedNetworkImage(
            imageUrl: "http://via.placeholder.com/350x150",
            progressIndicatorBuilder: (context, url, downloadProgress) => 
                    CircularProgressIndicator(value: downloadProgress.progress),
            errorWidget: (context, url, error) => Icon(Icons.error),
         ),
  3. Use CachedNetworkImage with imageBuilder for custom decorations

    develop

    If you need to use the cached image within a specific layout (like a BoxDecoration or a Container), use the imageBuilder callback. This provides the imageProvider which you can then pass to other widgets or decorations.

    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),
    ),
  4. Use CachedNetworkImage to display images with placeholders and error widgets

    develop

    The CachedNetworkImage widget is the primary way to display images from the internet while caching them locally. You can provide a placeholder widget to show while the image is loading and an errorWidget to display if the loading fails.

    ```dart
    CachedNetworkImage(
            imageUrl: "http://via.placeholder.com/350x150",
            placeholder: (context, url) => CircularProgressIndicator(),
            errorWidget: (context, url, error) => Icon(Icons.error),
         ),
  5. Configure cache management and HTTP headers

    develop

    You can customize how images are fetched and stored:

    • cacheManager: Pass a custom BaseCacheManager to control caching logic.
    • httpHeaders: A Map<String, String> of headers to include in the HTTP request for the image.
    • cacheKey: An optional key used to identify the image in the cache. If not provided, the imageUrl is used.
    • useOldImageOnUrlChange: If set to true, the widget will animate from the old image to the new image when the imageUrl changes.
  6. Configure placeholders, progress indicators, and error widgets

    develop

    You can customize the UI during different stages of the image loading lifecycle using builder functions:

    • placeholder: A PlaceholderWidgetBuilder called while the image is loading. Receives BuildContext and the url.
    • progressIndicatorBuilder: A ProgressIndicatorBuilder called during download. Receives BuildContext, url, and a DownloadProgress object containing expectedTotalBytes and cumulativeBytesLoaded.
    • errorWidget: A LoadingErrorWidgetBuilder called if loading fails. Receives BuildContext, url, and the error object.
    • imageBuilder: An ImageWidgetBuilder called after the image successfully loads. Receives BuildContext and the ImageProvider.
  7. Use CachedNetworkImageProvider for custom image loading

    develop
    The CachedNetworkImageProvider is a public ImageProvider that allows you to load network images using a cache. It can be used in any Flutter widget that accepts an ImageProvider (such as DecorationImage or CircleAvatar), providing an alternative to the standard CachedNetworkImage widget when you need more low-level control.