| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import '../util/image_utils.dart';
- /// 图片加载(支持本地与网络图片)
- class LoadImage extends StatelessWidget {
- const LoadImage(
- this.image, {
- super.key,
- this.width,
- this.height,
- this.fit = BoxFit.cover,
- this.format = ImageFormat.png,
- this.holderImg = 'none',
- this.cacheWidth,
- this.cacheHeight,
- });
- final String image;
- final double? width;
- final double? height;
- final BoxFit fit;
- final ImageFormat format;
- final String holderImg;
- final int? cacheWidth;
- final int? cacheHeight;
- @override
- Widget build(BuildContext context) {
- if (image.isEmpty) {
- return LoadAssetImage(
- holderImg,
- height: height,
- width: width,
- fit: fit,
- format: format,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- );
- }
- if (image.startsWith('http') || image.startsWith('https')) {
- final Widget widgetImage = LoadAssetImage(
- holderImg,
- height: height,
- width: width,
- fit: fit,
- );
- return CachedNetworkImage(
- imageUrl: image,
- placeholder: (_, __) => widgetImage,
- errorWidget: (_, __, dynamic error) => widgetImage,
- width: width,
- height: height,
- fit: fit,
- memCacheWidth: cacheWidth,
- memCacheHeight: cacheHeight,
- );
- } else {
- return LoadAssetImage(
- image,
- height: height,
- width: width,
- fit: fit,
- format: format,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- );
- }
- }
- }
- /// 加载本地资源图片
- class LoadAssetImage extends StatelessWidget {
- const LoadAssetImage(
- this.image, {
- super.key,
- this.width,
- this.height,
- this.cacheWidth,
- this.cacheHeight,
- this.fit,
- this.format = ImageFormat.png,
- this.color,
- });
- final String image;
- final double? width;
- final double? height;
- final int? cacheWidth;
- final int? cacheHeight;
- final BoxFit? fit;
- final ImageFormat format;
- final Color? color;
- @override
- Widget build(BuildContext context) {
- return Image.asset(
- image.startsWith("assets")
- ? image
- : ImageUtils.getImgPath(image, format: format),
- height: height,
- width: width,
- cacheWidth: cacheWidth,
- cacheHeight: cacheHeight,
- fit: fit,
- color: color,
- /// 忽略图片语义
- excludeFromSemantics: true,
- );
- }
- }
- class LoadBorderImage extends StatelessWidget {
- const LoadBorderImage(
- this.image, {
- super.key,
- this.width,
- this.height,
- this.holderImg = 'none',
- this.radius = 5,
- this.fit = BoxFit.fill,
- });
- final String image;
- final double? width;
- final double? height;
- final String holderImg;
- final double? radius;
- final BoxFit fit;
- @override
- Widget build(BuildContext context) {
- return Container(
- width: width,
- height: height,
- decoration: BoxDecoration(
- border: Border.all(
- color: Color(0xffEDEFF4),
- width: 1,
- style: BorderStyle.solid,
- ),
- borderRadius: BorderRadius.circular(radius ?? 5.r),
- image: DecorationImage(
- fit: fit,
- image: ImageUtils.getImageProvider(image, holderImg: holderImg),
- ),
- ),
- );
- }
- }
|