import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; /// 图片类型枚举 enum ImageType { svg, png, jpg } class KrLocalImage extends StatelessWidget { final String imageName; // 不含子目录的纯文件名,例如 "icon" final ImageType imageType; // 图片类型,可选,默认是 svg final double? width; final double? height; final BoxFit fit; final Color? color; const KrLocalImage({ Key? key, required this.imageName, this.imageType = ImageType.svg, // 默认类型为 SVG this.width, this.height, this.color, this.fit = BoxFit.contain, }) : super(key: key); @override Widget build(BuildContext context) { switch (imageType) { case ImageType.svg: return SvgPicture.asset( 'assets/images/$imageName.svg', width: width, height: height, fit: fit, colorFilter: color != null ? ColorFilter.mode(color!, BlendMode.srcIn) : null, ); case ImageType.png: case ImageType.jpg: return Image.asset( 'assets/images/$imageName.${imageType == ImageType.png ? 'png' : 'jpg'}', width: width, height: height, fit: fit, color: color, colorBlendMode: color != null ? BlendMode.srcIn : null, ); default: throw UnsupportedError('Unsupported image type: $imageType'); } } }