uploader.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-09-02 16:05:15
  4. * @LastEditors: WuXiangNan
  5. * @LastEditTime: 2021-04-09 12:01:10
  6. * @Description:
  7. */
  8. // import 'dart:ffi';
  9. import 'package:flutter/material.dart';
  10. import 'package:wisdom_cli/wisdom_cli.dart';
  11. class _Scope extends InheritedWidget {
  12. _Scope({
  13. Key? key,
  14. this.state,
  15. required this.child,
  16. this.generation,
  17. }) : super(key: key, child: child);
  18. final Widget child;
  19. final int? generation;
  20. final WUpLoaderState? state;
  21. @override
  22. bool updateShouldNotify(_Scope oldWidget) {
  23. return oldWidget.generation != this.generation;
  24. }
  25. }
  26. ///图片上传
  27. class WUpLoader extends StatefulWidget {
  28. WUpLoader({
  29. Key? key,
  30. required this.child,
  31. this.onChanged,
  32. }) : super(key: key);
  33. final Widget child;
  34. final void Function(List<FileEntity>)? onChanged;
  35. static WUpLoaderState? of(BuildContext context) {
  36. return context.dependOnInheritedWidgetOfExactType<_Scope>()?.state;
  37. }
  38. WUpLoader.wrap({
  39. Key? key,
  40. EdgeInsetsGeometry? padding,
  41. double? runSpacing,
  42. double? spacing,
  43. Widget Function(
  44. BuildContext context,
  45. int index,
  46. )?
  47. builder,
  48. Widget? selector,
  49. this.onChanged,
  50. }) : this.child = Builder(
  51. builder: (context) {
  52. final state = WUpLoader.of(context)!;
  53. final value = state.value;
  54. Widget current = Wrap(
  55. alignment: WrapAlignment.start,
  56. runAlignment: WrapAlignment.start,
  57. crossAxisAlignment: WrapCrossAlignment.center,
  58. runSpacing: runSpacing ?? 8.pt,
  59. spacing: spacing ?? 8.pt,
  60. children: [
  61. if (builder != null)
  62. for (var i = 0; i < value.length; i++) builder(context, i),
  63. if (selector != null) selector,
  64. ],
  65. );
  66. if (padding != EdgeInsets.zero)
  67. current = Padding(
  68. padding: padding ?? EdgeInsets.symmetric(vertical: 15.pt),
  69. child: current,
  70. );
  71. return current;
  72. },
  73. ),
  74. super(key: key);
  75. @override
  76. WUpLoaderState createState() => WUpLoaderState();
  77. }
  78. class WUpLoaderState extends State<WUpLoader> {
  79. int _generation = 0;
  80. List<FileEntity> _value = [];
  81. List<FileEntity> get value => _value;
  82. void _onChanged() {
  83. if (widget.onChanged != null) {
  84. widget.onChanged!(_value);
  85. }
  86. }
  87. @override
  88. Widget build(BuildContext context) {
  89. return _Scope(
  90. state: this,
  91. generation: _generation,
  92. child: widget.child,
  93. );
  94. }
  95. ///添加
  96. void add(FileEntity e) {
  97. _value.add(e);
  98. _generation++;
  99. _onChanged();
  100. setState(() {});
  101. }
  102. ///添加
  103. void addAll(List<FileEntity>? e) {
  104. if (e != null) {
  105. _value.addAll(e);
  106. _generation++;
  107. _onChanged();
  108. setState(() {});
  109. }
  110. }
  111. ///移除
  112. void remove(FileEntity e) {
  113. _value.remove(e);
  114. _generation++;
  115. _onChanged();
  116. setState(() {});
  117. }
  118. ///移除
  119. void removeAt(int? index) {
  120. if (index != null) {
  121. _value.removeAt(index);
  122. _generation++;
  123. _onChanged();
  124. setState(() {});
  125. }
  126. }
  127. }
  128. ///
  129. ///
  130. ///
  131. ///
  132. ///
  133. ///
  134. ///
  135. class WUpLoaderChild extends Builder {
  136. WUpLoaderChild.image({
  137. Key? key,
  138. double? size,
  139. BorderRadius? borderRadius,
  140. BoxBorder? border,
  141. Color? color,
  142. int? index,
  143. }) : super(
  144. key: key,
  145. builder: (context) {
  146. final state = WUpLoader.of(context)!;
  147. final value = state.value;
  148. final fileEntity = value[index!];
  149. void delete() async {
  150. state.removeAt(index);
  151. }
  152. List<ImageProvider> photoList =
  153. (value).map((e) => FileImage(e.file!)).toList();
  154. return WHandleView(
  155. key: key,
  156. width: size ?? 60.pt,
  157. height: size ?? 60.pt,
  158. onTap: () => WisPhotoViewUtil.show(photoList, index),
  159. onDelete: delete,
  160. borderRadius: borderRadius ?? BorderRadius.circular(4.pt),
  161. border: border,
  162. color: color,
  163. child: WImage(
  164. width: size ?? 60.pt,
  165. height: size ?? 60.pt,
  166. image: FileImage(fileEntity.file!),
  167. fit: BoxFit.cover,
  168. borderRadius: borderRadius ?? BorderRadius.circular(4.pt),
  169. ),
  170. );
  171. },
  172. );
  173. WUpLoaderChild.imagePicker({
  174. Key? key,
  175. double? size,
  176. void Function(List<FileEntity> res)? onTap,
  177. BorderRadius? borderRadius,
  178. BoxBorder? border,
  179. Widget? child,
  180. Color color = const Color(0xfff8f8f8),
  181. int maxCount = 9,
  182. WImageSource source = WImageSource.cameraAndGallery,
  183. }) : super(
  184. builder: (context) {
  185. final state = WUpLoader.of(context);
  186. final _maxCount = maxCount - (state?.value.length ?? 0);
  187. if (_maxCount == 0) {
  188. return Container();
  189. }
  190. void picker() async {
  191. final res = await WAssetPickerUtil.image(
  192. maxCount: _maxCount,
  193. source: source,
  194. );
  195. state?.addAll(res);
  196. if (onTap != null) {
  197. onTap(res);
  198. }
  199. }
  200. return WHandleView(
  201. key: key,
  202. width: size ?? 60.pt,
  203. height: size ?? 60.pt,
  204. borderRadius: borderRadius ?? BorderRadius.circular(4.pt),
  205. border: border ?? Border.all(color: Color(0xffe6e6e6)),
  206. color: color,
  207. child: Wisdom(
  208. alignment: Alignment.center,
  209. onTap: picker,
  210. child: child,
  211. ),
  212. );
  213. },
  214. );
  215. }