import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.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 { final String imageUrl; final double? width; final double? height; final BoxFit fit; final double borderRadius; final Widget? placeholder; final Widget? errorWidget; const CustomCachedImage({ super.key, required this.imageUrl, this.width, this.height, this.fit = BoxFit.cover, this.borderRadius = 0, this.placeholder, this.errorWidget, }); @override Widget build(BuildContext context) { return ClipRRect( borderRadius: BorderRadius.circular(borderRadius), child: CachedNetworkImage( imageUrl: imageUrl, width: width, height: height, memCacheWidth: _resolveMemCacheDimension(width), memCacheHeight: _resolveMemCacheDimension(height), fit: fit, placeholder: (context, url) => placeholder ?? _defaultPlaceholder(), errorWidget: (context, url, error) => errorWidget ?? _defaultErrorWidget(), ), ); } Widget _defaultPlaceholder() { return const SCRotatingDotsLoading(); } Widget _defaultErrorWidget() { return Center(child: Image.asset("sc_images/general/sc_icon_loading.png")); } }