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,
);
/// 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.
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
/// [BaseCacheManager] as the in memory [ImageCache] of the [ImageProvider].
/// [url] is used by both the disk and memory cache. The scale is only used
/// to clear the image from the [ImageCache].
static Future evictFromCache(
static Future<bool> evictFromCache(
String url, {
String? cacheKey,
BaseCacheManager? cacheManager,
double scale = 1.0,
double scale = 1,
}) async {
cacheManager = cacheManager ?? DefaultCacheManager();
await cacheManager.removeFile(cacheKey ?? url);
final effectiveCacheManager = cacheManager ?? DefaultCacheManager();
await effectiveCacheManager.removeFile(cacheKey ?? url);
return CachedNetworkImageProvider(url, scale: scale).evict();
}
final CachedNetworkImageProvider _image;
/// Option to use cachemanager with other settings
/// Option to use cacheManager with other settings
final BaseCacheManager? cacheManager;
/// 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.
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
/// 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
/// widget.
CachedNetworkImage({
Key? key,
super.key,
required this.imageUrl,
this.httpHeaders,
this.imageBuilder,
@ -204,9 +238,10 @@ class CachedNetworkImage extends StatelessWidget {
this.cacheKey,
this.maxWidthDiskCache,
this.maxHeightDiskCache,
this.errorListener,
ImageRenderMethodForWeb imageRenderMethodForWeb =
ImageRenderMethodForWeb.HtmlImage,
}) : _image = CachedNetworkImageProvider(
}) : _image = CachedNetworkImageProvider(
imageUrl,
headers: httpHeaders,
cacheManager: cacheManager,
@ -214,17 +249,17 @@ class CachedNetworkImage extends StatelessWidget {
imageRenderMethodForWeb: imageRenderMethodForWeb,
maxWidth: maxWidthDiskCache,
maxHeight: maxHeightDiskCache,
),
super(key: key);
errorListener: errorListener,
);
@override
Widget build(BuildContext context) {
var octoPlaceholderBuilder =
placeholder != null ? _octoPlaceholderBuilder : null;
var octoProgressIndicatorBuilder =
final octoProgressIndicatorBuilder =
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
///CachedNetworkImage.
if (octoPlaceholderBuilder == null &&
@ -277,7 +312,10 @@ class CachedNetworkImage extends StatelessWidget {
downloaded = progress.cumulativeBytesLoaded;
}
return progressIndicatorBuilder!(
context, imageUrl, DownloadProgress(imageUrl, totalSize, downloaded));
context,
imageUrl,
DownloadProgress(imageUrl, totalSize, downloaded),
);
}
Widget _octoErrorBuilder(