image_preview.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * @Author: XianKaiQun
  3. * @LastEditors : WuWei
  4. * @Date: 2020-11-04 14:32:20
  5. * @LastEditTime : 2022-01-19 10:47:18
  6. */
  7. import 'package:flutter/material.dart';
  8. import 'package:wisdom_cli/wisdom_cli.dart';
  9. ///[WImagePreview]嵌入式图片预览widget,
  10. ///提供`onDelete`事件,则右上角关闭小圈圈就会显示,不提供则不显示
  11. class WImagePreview extends StatelessWidget {
  12. const WImagePreview({
  13. Key? key,
  14. required this.images,
  15. required this.index,
  16. required this.onDelete,
  17. this.size,
  18. }) : super(key: key);
  19. final List<ImageProvider> images;
  20. final double? size;
  21. final int index;
  22. final void Function()? onDelete;
  23. ///一般来说,使用[WImagePreview]嵌入式图片预览widget时,没有意外的话都会直接构造一个列表,
  24. ///所以这里提供一个快速构建一个`List<Widget>`的方法,
  25. ///这样你可以使[WImagePreview]和
  26. static List<WImagePreview> buildList({
  27. List<ImageProvider>? images,
  28. void Function(int index)? onDelete,
  29. double? size,
  30. }) {
  31. return [
  32. if (images != null && images.length > 0)
  33. for (int i = 0; i < images.length; i++)
  34. WImagePreview(
  35. size: size,
  36. images: images,
  37. index: i,
  38. onDelete: onDelete == null ? null : () => onDelete(i),
  39. )
  40. ];
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return Wisdom.stack(
  45. width: size ?? 65.pt,
  46. height: size ?? 65.pt,
  47. clipBehavior: Clip.none,
  48. children: [
  49. Wisdom(
  50. onTap: () => WisPhotoViewUtil.show(images, index),
  51. padding: EdgeInsets.only(right: 5.pt, top: 5.pt),
  52. child: DecoratedBox(
  53. position: DecorationPosition.foreground,
  54. decoration: BoxDecoration(
  55. borderRadius: BorderRadius.circular(4.pt),
  56. border: Border.all(color: Colors.black12, width: 0.5.pt),
  57. ),
  58. child: WImage(
  59. image: images[index],
  60. borderRadius: BorderRadius.circular(4.pt),
  61. width: size ?? 65.pt,
  62. height: size ?? 65.pt,
  63. fit: BoxFit.cover,
  64. ),
  65. ),
  66. ),
  67. if (onDelete != null)
  68. Positioned(
  69. right: 0,
  70. top: 0,
  71. child: WClose(
  72. onTap: onDelete,
  73. backgroundColor: Colors.black.withOpacity(0.6),
  74. color: Colors.white,
  75. ),
  76. ),
  77. ],
  78. );
  79. }
  80. }