96 lines
2.8 KiB
Dart
96 lines
2.8 KiB
Dart
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:yumi/shared/tools/sc_network_image_utils.dart';
|
|
import 'package:yumi/ui_kit/components/sc_rotating_dots_loading.dart';
|
|
|
|
int? _resolveMemCacheDimension(double? logicalSize) {
|
|
if (logicalSize == null || !logicalSize.isFinite || logicalSize <= 0) {
|
|
return null;
|
|
}
|
|
final devicePixelRatio =
|
|
WidgetsBinding
|
|
.instance
|
|
.platformDispatcher
|
|
.implicitView
|
|
?.devicePixelRatio ??
|
|
2.0;
|
|
final pixelSize = (logicalSize * devicePixelRatio).round();
|
|
return pixelSize.clamp(1, 1920).toInt();
|
|
}
|
|
|
|
class CustomCachedImage extends StatelessWidget {
|
|
static final Set<String> _loggedLoadingUrls = <String>{};
|
|
static final Set<String> _loggedErrorUrls = <String>{};
|
|
|
|
final String imageUrl;
|
|
final double? width;
|
|
final double? height;
|
|
final BoxFit fit;
|
|
final double borderRadius;
|
|
final Widget? placeholder;
|
|
final Widget? errorWidget;
|
|
final String? debugName;
|
|
|
|
const CustomCachedImage({
|
|
super.key,
|
|
required this.imageUrl,
|
|
this.width,
|
|
this.height,
|
|
this.fit = BoxFit.cover,
|
|
this.borderRadius = 0,
|
|
this.placeholder,
|
|
this.errorWidget,
|
|
this.debugName,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final normalizedUrl = normalizeImageResourceUrl(imageUrl);
|
|
return ClipRRect(
|
|
borderRadius: BorderRadius.circular(borderRadius),
|
|
child: CachedNetworkImage(
|
|
imageUrl: normalizedUrl,
|
|
httpHeaders: buildNetworkImageHeaders(normalizedUrl),
|
|
width: width,
|
|
height: height,
|
|
memCacheWidth: _resolveMemCacheDimension(width),
|
|
memCacheHeight: _resolveMemCacheDimension(height),
|
|
fit: fit,
|
|
placeholder: (context, url) {
|
|
_debugLoading(normalizedUrl);
|
|
return placeholder ?? _defaultPlaceholder();
|
|
},
|
|
errorWidget: (context, url, error) {
|
|
_debugError(normalizedUrl, error);
|
|
return errorWidget ?? _defaultErrorWidget();
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void _debugLoading(String url) {
|
|
if (!kDebugMode || !_loggedLoadingUrls.add(url)) {
|
|
return;
|
|
}
|
|
final label = debugName == null ? '' : '[$debugName] ';
|
|
debugPrint('[NetworkImage][custom] ${label}loading url=$url');
|
|
}
|
|
|
|
void _debugError(String url, Object error) {
|
|
if (!kDebugMode || !_loggedErrorUrls.add('$url|$error')) {
|
|
return;
|
|
}
|
|
final label = debugName == null ? '' : '[$debugName] ';
|
|
debugPrint('[NetworkImage][custom] ${label}failed url=$url error=$error');
|
|
}
|
|
|
|
Widget _defaultPlaceholder() {
|
|
return const SCRotatingDotsLoading();
|
|
}
|
|
|
|
Widget _defaultErrorWidget() {
|
|
return Center(child: Image.asset("sc_images/general/sc_icon_loading.png"));
|
|
}
|
|
}
|