image_preview_file.dart 2.5 KB

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