load_image.dart
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:Parlando/util/image_utils.dart';
/// 图片加载(支持本地与网络图片)
class LoadImage extends StatelessWidget {
const LoadImage(this.image, {
Key? key,
this.width,
this.height,
this.fit = BoxFit.cover,
this.format = ImageFormat.png,
this.holderImg = 'none',
this.cacheWidth,
this.cacheHeight,
}) : super(key: key);
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 || image.startsWith('http')) {
final Widget _image = LoadAssetImage(holderImg, height: height, width: width, fit: fit);
return CachedNetworkImage(
imageUrl: image,
placeholder: (_, __) => _image,
errorWidget: (_, __, dynamic error) => _image,
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, {
Key? key,
this.width,
this.height,
this.cacheWidth,
this.cacheHeight,
this.fit,
this.format = ImageFormat.png,
this.color
}): super(key: key);
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(
ImageUtils.getImgPath(image, format: format),
height: height,
width: width,
cacheWidth: cacheWidth,
cacheHeight: cacheHeight,
fit: fit,
color: color,
/// 忽略图片语义
excludeFromSemantics: true,
);
}
}