From 21a00073573c11e2db2d238ddcdb3b24b21f63b8 Mon Sep 17 00:00:00 2001 From: minch93 Date: Sun, 3 Sep 2023 22:38:09 +0800 Subject: [PATCH 1/2] [FEATURE] Allow to customise the view of pickers --- example/lib/main.dart | 4 +++ lib/src/form_builder_file_picker.dart | 37 +++++++++++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ef3b412..d9d50bf 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -58,6 +58,10 @@ class MyHomePageState extends State { ), ) ], + customTypeViewerBuilder: (children) => Row( + mainAxisAlignment: MainAxisAlignment.end, + children: children, + ), onFileLoading: (val) { debugPrint(val.toString()); }, diff --git a/lib/src/form_builder_file_picker.dart b/lib/src/form_builder_file_picker.dart index 2476154..af85330 100644 --- a/lib/src/form_builder_file_picker.dart +++ b/lib/src/form_builder_file_picker.dart @@ -71,6 +71,9 @@ class FormBuilderFilePicker /// to support user interactions with the picked files. final FileViewerBuilder? customFileViewerBuilder; + /// Allow to customise the view of the pickers. + final Widget Function(List types)? customTypeViewerBuilder; + /// Creates field for image(s) from user device storage FormBuilderFilePicker({ //From Super @@ -98,6 +101,7 @@ class FormBuilderFilePicker this.onFileLoading, this.allowCompression = true, this.customFileViewerBuilder, + this.customTypeViewerBuilder }) : super( builder: (FormFieldState?> field) { final state = field as _FormBuilderFilePickerState; @@ -109,20 +113,11 @@ class FormBuilderFilePicker : null), child: Column( children: [ - Row( + customTypeViewerBuilder != null + ? customTypeViewerBuilder(state.getTypeSelectorActions(typeSelectors, field)) + : Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - ...typeSelectors.map( - (typeSelector) => InkWell( - onTap: state.enabled && - (null == state._remainingItemCount || - state._remainingItemCount! > 0) - ? () => state.pickFiles(field, typeSelector.type) - : null, - child: typeSelector.selector, - ), - ), - ], + children: state.getTypeSelectorActions(typeSelectors, field), ), const SizedBox(height: 3), customFileViewerBuilder != null @@ -305,6 +300,22 @@ class _FormBuilderFilePickerState extends FormBuilderFieldDecorationState< ); } + List getTypeSelectorActions(List typeSelectors, FormFieldState?> field) { + return [ + ...typeSelectors.map( + (typeSelector) => + InkWell( + onTap: enabled && + (null == _remainingItemCount || + _remainingItemCount! > 0) + ? () => pickFiles(field, typeSelector.type) + : null, + child: typeSelector.selector, + ), + ), + ]; + } + IconData getIconData(String fileExtension) { final lowerCaseFileExt = fileExtension.toLowerCase(); if (imageFileExts.contains(lowerCaseFileExt)) return Icons.image; From ee166392de6bc7630e85a07beb44b3c2b1a481e0 Mon Sep 17 00:00:00 2001 From: Danny Low Date: Mon, 4 Sep 2023 02:52:57 +0800 Subject: [PATCH 2/2] [TASK] Apply dart format --- lib/src/form_builder_file_picker.dart | 86 ++++++++++++++------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/lib/src/form_builder_file_picker.dart b/lib/src/form_builder_file_picker.dart index af85330..6b4d1f1 100644 --- a/lib/src/form_builder_file_picker.dart +++ b/lib/src/form_builder_file_picker.dart @@ -75,34 +75,35 @@ class FormBuilderFilePicker final Widget Function(List types)? customTypeViewerBuilder; /// Creates field for image(s) from user device storage - FormBuilderFilePicker({ - //From Super - super.key, - required super.name, - super.validator, - super.initialValue, - super.decoration, - super.onChanged, - super.valueTransformer, - super.enabled, - super.onSaved, - super.autovalidateMode = AutovalidateMode.disabled, - super.onReset, - super.focusNode, - this.maxFiles, - this.withData = kIsWeb, - this.withReadStream = false, - this.allowMultiple = false, - this.previewImages = true, - this.typeSelectors = const [ - TypeSelector(type: FileType.any, selector: Icon(Icons.add_circle)) - ], - this.allowedExtensions, - this.onFileLoading, - this.allowCompression = true, - this.customFileViewerBuilder, - this.customTypeViewerBuilder - }) : super( + FormBuilderFilePicker( + { + //From Super + super.key, + required super.name, + super.validator, + super.initialValue, + super.decoration, + super.onChanged, + super.valueTransformer, + super.enabled, + super.onSaved, + super.autovalidateMode = AutovalidateMode.disabled, + super.onReset, + super.focusNode, + this.maxFiles, + this.withData = kIsWeb, + this.withReadStream = false, + this.allowMultiple = false, + this.previewImages = true, + this.typeSelectors = const [ + TypeSelector(type: FileType.any, selector: Icon(Icons.add_circle)) + ], + this.allowedExtensions, + this.onFileLoading, + this.allowCompression = true, + this.customFileViewerBuilder, + this.customTypeViewerBuilder}) + : super( builder: (FormFieldState?> field) { final state = field as _FormBuilderFilePickerState; @@ -114,11 +115,13 @@ class FormBuilderFilePicker child: Column( children: [ customTypeViewerBuilder != null - ? customTypeViewerBuilder(state.getTypeSelectorActions(typeSelectors, field)) + ? customTypeViewerBuilder( + state.getTypeSelectorActions(typeSelectors, field)) : Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: state.getTypeSelectorActions(typeSelectors, field), - ), + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: state.getTypeSelectorActions( + typeSelectors, field), + ), const SizedBox(height: 3), customFileViewerBuilder != null ? customFileViewerBuilder.call(state._files, @@ -300,18 +303,17 @@ class _FormBuilderFilePickerState extends FormBuilderFieldDecorationState< ); } - List getTypeSelectorActions(List typeSelectors, FormFieldState?> field) { + List getTypeSelectorActions(List typeSelectors, + FormFieldState?> field) { return [ ...typeSelectors.map( - (typeSelector) => - InkWell( - onTap: enabled && - (null == _remainingItemCount || - _remainingItemCount! > 0) - ? () => pickFiles(field, typeSelector.type) - : null, - child: typeSelector.selector, - ), + (typeSelector) => InkWell( + onTap: enabled && + (null == _remainingItemCount || _remainingItemCount! > 0) + ? () => pickFiles(field, typeSelector.type) + : null, + child: typeSelector.selector, + ), ), ]; }