image_selecter.dart 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * @Author: XianKaiQun
  3. * @LastEditors : WuWei
  4. * @Date: 2020-11-04 10:43:03
  5. * @LastEditTime : 2023-05-25 15:55:58
  6. */
  7. import 'package:flutter/material.dart';
  8. import 'package:wisdom_cli/src/assets.gen.dart';
  9. import 'package:wisdom_cli/wisdom_cli.dart';
  10. class WImageSelecter extends StatefulWidget {
  11. const WImageSelecter({
  12. Key? key,
  13. this.size,
  14. required this.onSelected,
  15. required this.currentCount,
  16. this.maxCount = 9,
  17. this.source = WImageSource.cameraAndGallery,
  18. this.sourceCallBack,
  19. }) : super(key: key);
  20. ///Widget尺寸大小
  21. final double? size;
  22. ///选中图片后的回调,注意可能会返回`null`
  23. final void Function(List<FileEntity> values) onSelected;
  24. final int currentCount;
  25. /// [maxCount]最大选择数目,
  26. final int maxCount;
  27. /// [source] 图片来源
  28. final WImageSource source;
  29. /// [sourceCallBack] 回调图片来源(相机或相册)
  30. final void Function(WImageSource source)? sourceCallBack;
  31. @override
  32. _WImageSelecterState createState() => _WImageSelecterState();
  33. }
  34. class _WImageSelecterState extends State<WImageSelecter> {
  35. final FocusNode focusNode = FocusNode();
  36. @override
  37. void dispose() {
  38. focusNode.dispose();
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. final style = WTheme.of(context);
  44. final colorScheme = style.colorScheme;
  45. final maxCount = widget.maxCount;
  46. final currentCount = widget.currentCount;
  47. if (currentCount >= maxCount) return SizedBox(width: 0, height: 0);
  48. return Container(
  49. width: widget.size ?? 65.pt,
  50. height: widget.size ?? 65.pt,
  51. child: Wisdom.column(
  52. inkWell: WisBoxInkWell(
  53. onTap: () async {
  54. focusNode.requestFocus();
  55. final res = await WAssetPickerUtil.image(
  56. maxCount: maxCount - currentCount,
  57. source: widget.source,
  58. sourceCallBack: widget.sourceCallBack,
  59. );
  60. widget.onSelected(res);
  61. },
  62. focusNode: focusNode,
  63. ),
  64. margin: EdgeInsets.only(right: 5.pt, top: 5.pt),
  65. borderRadius: BorderRadius.circular(4.pt),
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. color: colorScheme.background,
  68. children: [
  69. if ((widget.maxCount) > 1) SizedBox(height: 8.pt),
  70. Image(
  71. image: AssetList.$image_png_image,
  72. width: 20.pt,
  73. ),
  74. if ((widget.maxCount) > 1)
  75. Text(
  76. '${widget.currentCount}/${widget.maxCount}',
  77. style: TextStyle(
  78. color: colorScheme.minor,
  79. height: 1.6,
  80. fontSize: 10.pt,
  81. ),
  82. ),
  83. ],
  84. ),
  85. );
  86. }
  87. }