load_image.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'dart:math';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:common_utils/common_utils.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:liftmanager/utils/image_utils.dart';
  6. import 'package:liftmanager/utils/utils.dart';
  7. /// 图片加载(支持本地与网络图片)
  8. class LoadImage extends StatelessWidget {
  9. const LoadImage(this.image,
  10. {Key key,
  11. this.width,
  12. this.height,
  13. this.fit: BoxFit.cover,
  14. this.format: "png",
  15. this.holderImg: "none"})
  16. : super(key: key);
  17. final String image;
  18. final double width;
  19. final double height;
  20. final BoxFit fit;
  21. final String format;
  22. final String holderImg;
  23. @override
  24. Widget build(BuildContext context) {
  25. if (TextUtil.isEmpty(image) || image == "null") {
  26. return LoadAssetImage(holderImg,
  27. height: height, width: width, fit: fit, format: format);
  28. } else {
  29. if (image.startsWith("http")) {
  30. return CachedNetworkImage(
  31. imageUrl: Utils.getImagePath(image),
  32. placeholder: (context, url) =>
  33. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  34. errorWidget: (context, url, error) =>
  35. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  36. width: width,
  37. height: height,
  38. fit: fit,
  39. );
  40. } else {
  41. return LoadAssetImage(image,
  42. height: height, width: width, fit: fit, format: format);
  43. }
  44. }
  45. }
  46. }
  47. /// 加载本地资源图片
  48. class LoadAssetImage extends StatelessWidget {
  49. const LoadAssetImage(this.image,
  50. {Key key,
  51. this.width: 25,
  52. this.height: 25,
  53. this.fit,
  54. this.format: 'png',
  55. this.color})
  56. : super(key: key);
  57. final String image;
  58. final double width;
  59. final double height;
  60. final BoxFit fit;
  61. final String format;
  62. final Color color;
  63. @override
  64. Widget build(BuildContext context) {
  65. return Image.asset(
  66. ImageUtils.getImgPath(image, format: format),
  67. height: height,
  68. width: width,
  69. fit: fit,
  70. color: color,
  71. );
  72. }
  73. }
  74. /// 加载网络图片,并进行异常处理,对路径进行验证,如果是相对路径,就拼接前缀,如果是绝对路径就直接显示
  75. class LoadNetworkImage extends StatelessWidget {
  76. const LoadNetworkImage(this.image,
  77. {Key key,
  78. this.width,
  79. this.height,
  80. this.fit: BoxFit.cover,
  81. this.isWater = false,
  82. this.format: "png",
  83. this.holderImg: "brand_default"})
  84. : super(key: key);
  85. final String image;
  86. final double width;
  87. final double height;
  88. final BoxFit fit;
  89. final String format;
  90. final String holderImg;
  91. final bool isWater;
  92. @override
  93. Widget build(BuildContext context) {
  94. if (TextUtil.isEmpty(image) || image == "null") {
  95. return LoadAssetImage(holderImg,
  96. height: height, width: width, fit: fit, format: format);
  97. } else {
  98. return CachedNetworkImage(
  99. imageUrl: Utils.getImagePath(image, isWater: isWater),
  100. placeholder: (context, url) =>
  101. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  102. errorWidget: (context, url, error) =>
  103. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  104. width: width,
  105. height: height,
  106. fit: fit,
  107. );
  108. }
  109. }
  110. }
  111. class LoadNetworkImageAlt extends StatelessWidget {
  112. LoadNetworkImageAlt(
  113. this.image, {
  114. Key key,
  115. this.width,
  116. this.height,
  117. this.fit: BoxFit.cover,
  118. this.isWater = false,
  119. this.format: "png",
  120. }) : super(key: key);
  121. final String image;
  122. final double width;
  123. final double height;
  124. final BoxFit fit;
  125. final String format;
  126. // final String holderImg = "brand_default${Random().nextInt(4) + 1}";
  127. final String holderImg = "brand_default${1}";
  128. final bool isWater;
  129. @override
  130. Widget build(BuildContext context) {
  131. if (TextUtil.isEmpty(image) || image == "null") {
  132. return LoadAssetImage(holderImg,
  133. height: height, width: width, fit: fit, format: format);
  134. } else {
  135. return CachedNetworkImage(
  136. imageUrl: Utils.getImagePath(image, isWater: isWater),
  137. placeholder: (context, url) =>
  138. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  139. errorWidget: (context, url, error) =>
  140. LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  141. width: width,
  142. height: height,
  143. fit: fit,
  144. );
  145. }
  146. }
  147. }