getAssetImg static method
构建一个 asset Image widget,带淡入动画、加载占位与解码尺寸缓存。
图片路径为 assets/images/$imagePath.${format.value}。
errorWidget 为加载失败时的占位(默认 broken_image 图标)。
Implementation
static Widget getAssetImg({
required String imagePath,
BoxFit boxFit = BoxFit.cover,
double? height,
double? width,
Widget? errorWidget,
Color? color,
ImageFormat format = ImageFormat.png,
}) {
return Image.asset(
'assets/images/$imagePath.${format.value}',
fit: boxFit,
height: height,
width: width,
color: color,
cacheHeight: height?.toInt(),
cacheWidth: width?.toInt(),
errorBuilder:
(BuildContext context, Object error, StackTrace? stackTrace) {
return errorWidget ?? const Icon(Icons.broken_image);
},
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
if (wasSynchronouslyLoaded) return child;
return AnimatedOpacity(
opacity: frame == null ? 0 : 1,
duration: const Duration(seconds: 1),
curve: Curves.easeOut,
child: child,
);
},
);
}