load_image.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import '../util/image_utils.dart';
  5. /// 图片加载(支持本地与网络图片)
  6. class LoadImage extends StatelessWidget {
  7. const LoadImage(
  8. this.image, {
  9. super.key,
  10. this.width,
  11. this.height,
  12. this.fit = BoxFit.cover,
  13. this.format = ImageFormat.png,
  14. this.holderImg = 'none',
  15. this.cacheWidth,
  16. this.cacheHeight,
  17. });
  18. final String image;
  19. final double? width;
  20. final double? height;
  21. final BoxFit fit;
  22. final ImageFormat format;
  23. final String holderImg;
  24. final int? cacheWidth;
  25. final int? cacheHeight;
  26. @override
  27. Widget build(BuildContext context) {
  28. if (image.isEmpty) {
  29. return LoadAssetImage(
  30. holderImg,
  31. height: height,
  32. width: width,
  33. fit: fit,
  34. format: format,
  35. cacheWidth: cacheWidth,
  36. cacheHeight: cacheHeight,
  37. );
  38. }
  39. if (image.startsWith('http') || image.startsWith('https')) {
  40. final Widget widgetImage = LoadAssetImage(
  41. holderImg,
  42. height: height,
  43. width: width,
  44. fit: fit,
  45. );
  46. return CachedNetworkImage(
  47. imageUrl: image,
  48. placeholder: (_, __) => widgetImage,
  49. errorWidget: (_, __, dynamic error) => widgetImage,
  50. width: width,
  51. height: height,
  52. fit: fit,
  53. memCacheWidth: cacheWidth,
  54. memCacheHeight: cacheHeight,
  55. );
  56. } else {
  57. return LoadAssetImage(
  58. image,
  59. height: height,
  60. width: width,
  61. fit: fit,
  62. format: format,
  63. cacheWidth: cacheWidth,
  64. cacheHeight: cacheHeight,
  65. );
  66. }
  67. }
  68. }
  69. /// 加载本地资源图片
  70. class LoadAssetImage extends StatelessWidget {
  71. const LoadAssetImage(
  72. this.image, {
  73. super.key,
  74. this.width,
  75. this.height,
  76. this.cacheWidth,
  77. this.cacheHeight,
  78. this.fit,
  79. this.format = ImageFormat.png,
  80. this.color,
  81. });
  82. final String image;
  83. final double? width;
  84. final double? height;
  85. final int? cacheWidth;
  86. final int? cacheHeight;
  87. final BoxFit? fit;
  88. final ImageFormat format;
  89. final Color? color;
  90. @override
  91. Widget build(BuildContext context) {
  92. return Image.asset(
  93. image.startsWith("assets")
  94. ? image
  95. : ImageUtils.getImgPath(image, format: format),
  96. height: height,
  97. width: width,
  98. cacheWidth: cacheWidth,
  99. cacheHeight: cacheHeight,
  100. fit: fit,
  101. color: color,
  102. /// 忽略图片语义
  103. excludeFromSemantics: true,
  104. );
  105. }
  106. }
  107. class LoadBorderImage extends StatelessWidget {
  108. const LoadBorderImage(
  109. this.image, {
  110. super.key,
  111. this.width,
  112. this.height,
  113. this.holderImg = 'none',
  114. this.radius = 5,
  115. this.fit = BoxFit.fill,
  116. });
  117. final String image;
  118. final double? width;
  119. final double? height;
  120. final String holderImg;
  121. final double? radius;
  122. final BoxFit fit;
  123. @override
  124. Widget build(BuildContext context) {
  125. return Container(
  126. width: width,
  127. height: height,
  128. decoration: BoxDecoration(
  129. border: Border.all(
  130. color: Color(0xffEDEFF4),
  131. width: 1,
  132. style: BorderStyle.solid,
  133. ),
  134. borderRadius: BorderRadius.circular(radius ?? 5.r),
  135. image: DecorationImage(
  136. fit: fit,
  137. image: ImageUtils.getImageProvider(image, holderImg: holderImg),
  138. ),
  139. ),
  140. );
  141. }
  142. }