123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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,
- );
- }
- }
- }
|