load_image.dart 3.3 KB

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