load_image.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /// 图片加载(支持本地与网络图片)
  6. class LoadImage extends StatelessWidget {
  7. const LoadImage(this.image, {
  8. Key key,
  9. this.width,
  10. this.height,
  11. this.fit: BoxFit.cover,
  12. this.format: "png",
  13. this.holderImg: "none"
  14. }): super(key: key);
  15. final String image;
  16. final double width;
  17. final double height;
  18. final BoxFit fit;
  19. final String format;
  20. final String holderImg;
  21. @override
  22. Widget build(BuildContext context) {
  23. if (TextUtil.isEmpty(image) || image == "null"){
  24. return LoadAssetImage(holderImg,
  25. height: height,
  26. width: width,
  27. fit: fit,
  28. format: format
  29. );
  30. }else {
  31. if (image.startsWith("http")){
  32. return CachedNetworkImage(
  33. imageUrl: image,
  34. placeholder: (context, url) => LoadAssetImage(holderImg, height: height, width: width, fit: fit),
  35. errorWidget: (context, url, error) => 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,
  43. width: width,
  44. fit: fit,
  45. format: format
  46. );
  47. }
  48. }
  49. }
  50. }
  51. /// 加载本地资源图片
  52. class LoadAssetImage extends StatelessWidget {
  53. const LoadAssetImage(this.image, {
  54. Key key,
  55. this.width:25,
  56. this.height:25,
  57. this.fit,
  58. this.format: 'png',
  59. this.color
  60. }): super(key: key);
  61. final String image;
  62. final double width;
  63. final double height;
  64. final BoxFit fit;
  65. final String format;
  66. final Color color;
  67. @override
  68. Widget build(BuildContext context) {
  69. return Image.asset(
  70. ImageUtils.getImgPath(image, format: format),
  71. height: height,
  72. width: width,
  73. fit: fit,
  74. color: color,
  75. );
  76. }
  77. }