/* * @Author: XianKaiQun * @Date: 2020-09-02 16:05:15 * @LastEditors: WuXiangNan * @LastEditTime: 2021-04-09 12:01:10 * @Description: */ // import 'dart:ffi'; import 'package:flutter/material.dart'; import 'package:wisdom_cli/wisdom_cli.dart'; class _Scope extends InheritedWidget { _Scope({ Key? key, this.state, required this.child, this.generation, }) : super(key: key, child: child); final Widget child; final int? generation; final WUpLoaderState? state; @override bool updateShouldNotify(_Scope oldWidget) { return oldWidget.generation != this.generation; } } ///图片上传 class WUpLoader extends StatefulWidget { WUpLoader({ Key? key, required this.child, this.onChanged, }) : super(key: key); final Widget child; final void Function(List)? onChanged; static WUpLoaderState? of(BuildContext context) { return context.dependOnInheritedWidgetOfExactType<_Scope>()?.state; } WUpLoader.wrap({ Key? key, EdgeInsetsGeometry? padding, double? runSpacing, double? spacing, Widget Function( BuildContext context, int index, )? builder, Widget? selector, this.onChanged, }) : this.child = Builder( builder: (context) { final state = WUpLoader.of(context)!; final value = state.value; Widget current = Wrap( alignment: WrapAlignment.start, runAlignment: WrapAlignment.start, crossAxisAlignment: WrapCrossAlignment.center, runSpacing: runSpacing ?? 8.pt, spacing: spacing ?? 8.pt, children: [ if (builder != null) for (var i = 0; i < value.length; i++) builder(context, i), if (selector != null) selector, ], ); if (padding != EdgeInsets.zero) current = Padding( padding: padding ?? EdgeInsets.symmetric(vertical: 15.pt), child: current, ); return current; }, ), super(key: key); @override WUpLoaderState createState() => WUpLoaderState(); } class WUpLoaderState extends State { int _generation = 0; List _value = []; List get value => _value; void _onChanged() { if (widget.onChanged != null) { widget.onChanged!(_value); } } @override Widget build(BuildContext context) { return _Scope( state: this, generation: _generation, child: widget.child, ); } ///添加 void add(FileEntity e) { _value.add(e); _generation++; _onChanged(); setState(() {}); } ///添加 void addAll(List? e) { if (e != null) { _value.addAll(e); _generation++; _onChanged(); setState(() {}); } } ///移除 void remove(FileEntity e) { _value.remove(e); _generation++; _onChanged(); setState(() {}); } ///移除 void removeAt(int? index) { if (index != null) { _value.removeAt(index); _generation++; _onChanged(); setState(() {}); } } } /// /// /// /// /// /// /// class WUpLoaderChild extends Builder { WUpLoaderChild.image({ Key? key, double? size, BorderRadius? borderRadius, BoxBorder? border, Color? color, int? index, }) : super( key: key, builder: (context) { final state = WUpLoader.of(context)!; final value = state.value; final fileEntity = value[index!]; void delete() async { state.removeAt(index); } List photoList = (value).map((e) => FileImage(e.file!)).toList(); return WHandleView( key: key, width: size ?? 60.pt, height: size ?? 60.pt, onTap: () => WisPhotoViewUtil.show(photoList, index), onDelete: delete, borderRadius: borderRadius ?? BorderRadius.circular(4.pt), border: border, color: color, child: WImage( width: size ?? 60.pt, height: size ?? 60.pt, image: FileImage(fileEntity.file!), fit: BoxFit.cover, borderRadius: borderRadius ?? BorderRadius.circular(4.pt), ), ); }, ); WUpLoaderChild.imagePicker({ Key? key, double? size, void Function(List res)? onTap, BorderRadius? borderRadius, BoxBorder? border, Widget? child, Color color = const Color(0xfff8f8f8), int maxCount = 9, WImageSource source = WImageSource.cameraAndGallery, }) : super( builder: (context) { final state = WUpLoader.of(context); final _maxCount = maxCount - (state?.value.length ?? 0); if (_maxCount == 0) { return Container(); } void picker() async { final res = await WAssetPickerUtil.image( maxCount: _maxCount, source: source, ); state?.addAll(res); if (onTap != null) { onTap(res); } } return WHandleView( key: key, width: size ?? 60.pt, height: size ?? 60.pt, borderRadius: borderRadius ?? BorderRadius.circular(4.pt), border: border ?? Border.all(color: Color(0xffe6e6e6)), color: color, child: Wisdom( alignment: Alignment.center, onTap: picker, child: child, ), ); }, ); }