my_load_image.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:ftrecruiter/comm/utils/image_utils.dart';
  4. /// 图片加载(支持本地与网络图片)
  5. class MyLoadImage extends StatelessWidget {
  6. final String image;
  7. final double? width;
  8. final double? height;
  9. final BoxFit fit;
  10. final String placeholderPath;
  11. final int? cacheWidth;
  12. final int? cacheHeight;
  13. bool? isCircle = false;
  14. double? borderWidth = 0;
  15. Color? borderColor = Colors.transparent;
  16. VoidCallback? onClick;
  17. double? cornerRadius = 0;
  18. MyLoadImage(
  19. this.image, {
  20. Key? key,
  21. this.width,
  22. this.height,
  23. this.fit = BoxFit.cover,
  24. this.placeholderPath = '',
  25. this.cacheWidth,
  26. this.cacheHeight,
  27. this.isCircle,
  28. this.cornerRadius,
  29. this.borderColor,
  30. this.borderWidth,
  31. this.onClick,
  32. }) : super(key: key) {
  33. if (isCircle != null) {
  34. if (isCircle ?? true) {
  35. cornerRadius = width ?? 0 / 2;
  36. }
  37. }
  38. onClick ??= () {};
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. if (image.isEmpty || image.startsWith('http')) {
  43. final Widget placeholder = MyAssetImage(placeholderPath, height: height, width: width, fit: fit);
  44. return Container(
  45. decoration: BoxDecoration(
  46. border: Border.all(width: borderWidth ?? 0, color: borderColor ?? Colors.transparent),
  47. borderRadius: BorderRadius.all(Radius.circular(cornerRadius ?? 0)),
  48. ),
  49. child: GestureDetector(
  50. onTap: onClick,
  51. child: ClipRRect(
  52. borderRadius: BorderRadius.circular(cornerRadius ?? 0),
  53. child: CachedNetworkImage(
  54. imageUrl: image,
  55. placeholder: (_, __) => placeholder,
  56. errorWidget: (_, __, dynamic error) => placeholder,
  57. width: width,
  58. height: height,
  59. fit: fit,
  60. memCacheWidth: cacheWidth,
  61. memCacheHeight: cacheHeight,
  62. )),
  63. ));
  64. } else {
  65. return GestureDetector(
  66. onTap: onClick,
  67. child: MyAssetImage(
  68. image,
  69. height: height,
  70. width: width,
  71. fit: fit,
  72. cacheWidth: cacheWidth,
  73. cacheHeight: cacheHeight,
  74. ),
  75. );
  76. }
  77. }
  78. }
  79. /// 加载本地资源图片
  80. class MyAssetImage extends StatelessWidget {
  81. const MyAssetImage(this.image,
  82. {Key? key, this.width, this.height, this.cacheWidth, this.cacheHeight, this.fit, this.color})
  83. : super(key: key);
  84. final String image;
  85. final double? width;
  86. final double? height;
  87. final int? cacheWidth;
  88. final int? cacheHeight;
  89. final BoxFit? fit;
  90. final Color? color;
  91. @override
  92. Widget build(BuildContext context) {
  93. return Image.asset(
  94. ImageUtils.getImgPath(image),
  95. height: height,
  96. width: width,
  97. cacheWidth: cacheWidth,
  98. cacheHeight: cacheHeight,
  99. fit: fit,
  100. color: color,
  101. /// 忽略图片语义
  102. excludeFromSemantics: true,
  103. );
  104. }
  105. }