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