目的是为了整合asset图片显示、以及远程url图片显示。
增加依赖cached_network_image
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class NFTimage extends StatelessWidget {
final double? width;
final double? height;
final double? radius;
final BoxFit? fit;
final Color? color;
final ImageProvider image;
NFTimage.asset(
String name, {
Key? key,
this.width,
this.height,
this.color,
this.radius,
this.fit,
}) : image = Image.asset('images/common/$name').image,
super(key: key);
/*
NFTimage.asset -> image
也可以用这种内部实现方案
image = ResizeImage.resizeIfNeeded(
cacheWidth,
cacheHeight,
scale != null
? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
: AssetImage(name, bundle: bundle, package: package),
)
*/
NFTimage.network(String url,
{Key? key,
this.width,
this.height,
this.color,
this.radius,
this.fit = BoxFit.cover})
: image = CachedNetworkImageProvider(url),
super(key: key);
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius:
radius != null ? BorderRadius.circular(radius!) : BorderRadius.zero,
child: Image(
image: image,
width: width,
height: height,
color: color,
fit: fit,
),
);
}
}