Merge upstream changes to local CachedNetworkImage

This commit is contained in:
Ming Ming 2024-05-28 23:55:51 +08:00
parent 4e8d8d6768
commit 70932528a0

View file

@ -13,26 +13,57 @@ typedef ImageWidgetBuilder = Widget Function(
ImageProvider imageProvider, ImageProvider imageProvider,
); );
/// Builder function to create a placeholder widget. The function is called
/// once while the ImageProvider is loading the image.
typedef PlaceholderWidgetBuilder = Widget Function(
BuildContext context,
String url,
);
/// Builder function to create a progress indicator widget. The function is
/// called every time a chuck of the image is downloaded from the web, but at
/// least once during image loading.
typedef ProgressIndicatorBuilder = Widget Function(
BuildContext context,
String url,
DownloadProgress progress,
);
/// Builder function to create an error widget. This builder is called when
/// the image failed loading, for example due to a 404 NotFound exception.
typedef LoadingErrorWidgetBuilder = Widget Function(
BuildContext context,
String url,
Object error,
);
/// Image widget to show NetworkImage with caching functionality. /// Image widget to show NetworkImage with caching functionality.
class CachedNetworkImage extends StatelessWidget { class CachedNetworkImage extends StatelessWidget {
/// Get the current log level of the cache manager.
static CacheManagerLogLevel get logLevel => CacheManager.logLevel;
/// Set the log level of the cache manager to a [CacheManagerLogLevel].
static set logLevel(CacheManagerLogLevel level) =>
CacheManager.logLevel = level;
/// Evict an image from both the disk file based caching system of the /// Evict an image from both the disk file based caching system of the
/// [BaseCacheManager] as the in memory [ImageCache] of the [ImageProvider]. /// [BaseCacheManager] as the in memory [ImageCache] of the [ImageProvider].
/// [url] is used by both the disk and memory cache. The scale is only used /// [url] is used by both the disk and memory cache. The scale is only used
/// to clear the image from the [ImageCache]. /// to clear the image from the [ImageCache].
static Future evictFromCache( static Future<bool> evictFromCache(
String url, { String url, {
String? cacheKey, String? cacheKey,
BaseCacheManager? cacheManager, BaseCacheManager? cacheManager,
double scale = 1.0, double scale = 1,
}) async { }) async {
cacheManager = cacheManager ?? DefaultCacheManager(); final effectiveCacheManager = cacheManager ?? DefaultCacheManager();
await cacheManager.removeFile(cacheKey ?? url); await effectiveCacheManager.removeFile(cacheKey ?? url);
return CachedNetworkImageProvider(url, scale: scale).evict(); return CachedNetworkImageProvider(url, scale: scale).evict();
} }
final CachedNetworkImageProvider _image; final CachedNetworkImageProvider _image;
/// Option to use cachemanager with other settings /// Option to use cacheManager with other settings
final BaseCacheManager? cacheManager; final BaseCacheManager? cacheManager;
/// The target image that is displayed. /// The target image that is displayed.
@ -171,12 +202,15 @@ class CachedNetworkImage extends StatelessWidget {
/// Will resize the image and store the resized image in the disk cache. /// Will resize the image and store the resized image in the disk cache.
final int? maxHeightDiskCache; final int? maxHeightDiskCache;
/// Listener to be called when images fails to load.
final ValueChanged<Object>? errorListener;
/// CachedNetworkImage shows a network image using a caching mechanism. It also /// CachedNetworkImage shows a network image using a caching mechanism. It also
/// provides support for a placeholder, showing an error and fading into the /// provides support for a placeholder, showing an error and fading into the
/// loaded image. Next to that it supports most features of a default Image /// loaded image. Next to that it supports most features of a default Image
/// widget. /// widget.
CachedNetworkImage({ CachedNetworkImage({
Key? key, super.key,
required this.imageUrl, required this.imageUrl,
this.httpHeaders, this.httpHeaders,
this.imageBuilder, this.imageBuilder,
@ -204,9 +238,10 @@ class CachedNetworkImage extends StatelessWidget {
this.cacheKey, this.cacheKey,
this.maxWidthDiskCache, this.maxWidthDiskCache,
this.maxHeightDiskCache, this.maxHeightDiskCache,
this.errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb = ImageRenderMethodForWeb imageRenderMethodForWeb =
ImageRenderMethodForWeb.HtmlImage, ImageRenderMethodForWeb.HtmlImage,
}) : _image = CachedNetworkImageProvider( }) : _image = CachedNetworkImageProvider(
imageUrl, imageUrl,
headers: httpHeaders, headers: httpHeaders,
cacheManager: cacheManager, cacheManager: cacheManager,
@ -214,17 +249,17 @@ class CachedNetworkImage extends StatelessWidget {
imageRenderMethodForWeb: imageRenderMethodForWeb, imageRenderMethodForWeb: imageRenderMethodForWeb,
maxWidth: maxWidthDiskCache, maxWidth: maxWidthDiskCache,
maxHeight: maxHeightDiskCache, maxHeight: maxHeightDiskCache,
), errorListener: errorListener,
super(key: key); );
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var octoPlaceholderBuilder = var octoPlaceholderBuilder =
placeholder != null ? _octoPlaceholderBuilder : null; placeholder != null ? _octoPlaceholderBuilder : null;
var octoProgressIndicatorBuilder = final octoProgressIndicatorBuilder =
progressIndicatorBuilder != null ? _octoProgressIndicatorBuilder : null; progressIndicatorBuilder != null ? _octoProgressIndicatorBuilder : null;
///If there is no placeholer OctoImage does not fade, so always set an ///If there is no placeholder OctoImage does not fade, so always set an
///(empty) placeholder as this always used to be the behaviour of ///(empty) placeholder as this always used to be the behaviour of
///CachedNetworkImage. ///CachedNetworkImage.
if (octoPlaceholderBuilder == null && if (octoPlaceholderBuilder == null &&
@ -277,7 +312,10 @@ class CachedNetworkImage extends StatelessWidget {
downloaded = progress.cumulativeBytesLoaded; downloaded = progress.cumulativeBytesLoaded;
} }
return progressIndicatorBuilder!( return progressIndicatorBuilder!(
context, imageUrl, DownloadProgress(imageUrl, totalSize, downloaded)); context,
imageUrl,
DownloadProgress(imageUrl, totalSize, downloaded),
);
} }
Widget _octoErrorBuilder( Widget _octoErrorBuilder(