import 'dart:math'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:common_utils/common_utils.dart'; import 'package:flutter/material.dart'; import 'package:liftmanager/utils/image_utils.dart'; import 'package:liftmanager/utils/utils.dart'; /// 图片加载(支持本地与网络图片) class LoadImage extends StatelessWidget { const LoadImage(this.image, {Key key, this.width, this.height, this.fit: BoxFit.cover, this.format: "png", this.holderImg: "none"}) : super(key: key); final String image; final double width; final double height; final BoxFit fit; final String format; final String holderImg; @override Widget build(BuildContext context) { if (TextUtil.isEmpty(image) || image == "null") { return LoadAssetImage(holderImg, height: height, width: width, fit: fit, format: format); } else { if (image.startsWith("http")) { return CachedNetworkImage( imageUrl: Utils.getImagePath(image), placeholder: (context, url) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), errorWidget: (context, url, error) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), width: width, height: height, fit: fit, ); } else { return LoadAssetImage(image, height: height, width: width, fit: fit, format: format); } } } } /// 加载本地资源图片 class LoadAssetImage extends StatelessWidget { const LoadAssetImage(this.image, {Key key, this.width: 25, this.height: 25, this.fit, this.format: 'png', this.color}) : super(key: key); final String image; final double width; final double height; final BoxFit fit; final String format; final Color color; @override Widget build(BuildContext context) { return Image.asset( ImageUtils.getImgPath(image, format: format), height: height, width: width, fit: fit, color: color, ); } } /// 加载网络图片,并进行异常处理,对路径进行验证,如果是相对路径,就拼接前缀,如果是绝对路径就直接显示 class LoadNetworkImage extends StatelessWidget { const LoadNetworkImage(this.image, {Key key, this.width, this.height, this.fit: BoxFit.cover, this.isWater = false, this.format: "png", this.holderImg: "brand_default"}) : super(key: key); final String image; final double width; final double height; final BoxFit fit; final String format; final String holderImg; final bool isWater; @override Widget build(BuildContext context) { if (TextUtil.isEmpty(image) || image == "null") { return LoadAssetImage(holderImg, height: height, width: width, fit: fit, format: format); } else { return CachedNetworkImage( imageUrl: Utils.getImagePath(image, isWater: isWater), placeholder: (context, url) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), errorWidget: (context, url, error) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), width: width, height: height, fit: fit, ); } } } class LoadNetworkImageAlt extends StatelessWidget { LoadNetworkImageAlt( this.image, { Key key, this.width, this.height, this.fit: BoxFit.cover, this.isWater = false, this.format: "png", }) : super(key: key); final String image; final double width; final double height; final BoxFit fit; final String format; // final String holderImg = "brand_default${Random().nextInt(4) + 1}"; final String holderImg = "brand_default${1}"; final bool isWater; @override Widget build(BuildContext context) { if (TextUtil.isEmpty(image) || image == "null") { return LoadAssetImage(holderImg, height: height, width: width, fit: fit, format: format); } else { return CachedNetworkImage( imageUrl: Utils.getImagePath(image, isWater: isWater), placeholder: (context, url) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), errorWidget: (context, url, error) => LoadAssetImage(holderImg, height: height, width: width, fit: fit), width: width, height: height, fit: fit, ); } } }