image_utils.dart 820 B

1234567891011121314151617181920212223242526272829
  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:flutter/widgets.dart';
  5. //用于加载本地图片的工具类
  6. class ImageUtils {
  7. static ImageProvider getAssetImage(String name) {
  8. return AssetImage(getImgPath(name));
  9. }
  10. static String getImgPath(String name) {
  11. if(name.startsWith("assets/images/")){
  12. name = name.substring(14);
  13. }
  14. if (name.startsWith('/')) {
  15. name = name.substring(1);
  16. }
  17. return 'assets/images/$name';
  18. }
  19. static ImageProvider getImageProvider(String? imageUrl, {String holderImg = 'none'}) {
  20. if (TextUtil.isEmpty(imageUrl)) {
  21. return AssetImage(getImgPath(holderImg));
  22. }
  23. return CachedNetworkImageProvider(imageUrl!);
  24. }
  25. }