Skip to content

Commit d76cf4d

Browse files
Merge pull request #69 from lowminchien/main
Allow to customise the view of pickers
2 parents eee7332 + ee16639 commit d76cf4d

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

example/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class MyHomePageState extends State<MyHomePage> {
5858
),
5959
)
6060
],
61+
customTypeViewerBuilder: (children) => Row(
62+
mainAxisAlignment: MainAxisAlignment.end,
63+
children: children,
64+
),
6165
onFileLoading: (val) {
6266
debugPrint(val.toString());
6367
},

lib/src/form_builder_file_picker.dart

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,39 @@ class FormBuilderFilePicker
7171
/// to support user interactions with the picked files.
7272
final FileViewerBuilder? customFileViewerBuilder;
7373

74+
/// Allow to customise the view of the pickers.
75+
final Widget Function(List<Widget> types)? customTypeViewerBuilder;
76+
7477
/// Creates field for image(s) from user device storage
75-
FormBuilderFilePicker({
76-
//From Super
77-
super.key,
78-
required super.name,
79-
super.validator,
80-
super.initialValue,
81-
super.decoration,
82-
super.onChanged,
83-
super.valueTransformer,
84-
super.enabled,
85-
super.onSaved,
86-
super.autovalidateMode = AutovalidateMode.disabled,
87-
super.onReset,
88-
super.focusNode,
89-
this.maxFiles,
90-
this.withData = kIsWeb,
91-
this.withReadStream = false,
92-
this.allowMultiple = false,
93-
this.previewImages = true,
94-
this.typeSelectors = const [
95-
TypeSelector(type: FileType.any, selector: Icon(Icons.add_circle))
96-
],
97-
this.allowedExtensions,
98-
this.onFileLoading,
99-
this.allowCompression = true,
100-
this.customFileViewerBuilder,
101-
}) : super(
78+
FormBuilderFilePicker(
79+
{
80+
//From Super
81+
super.key,
82+
required super.name,
83+
super.validator,
84+
super.initialValue,
85+
super.decoration,
86+
super.onChanged,
87+
super.valueTransformer,
88+
super.enabled,
89+
super.onSaved,
90+
super.autovalidateMode = AutovalidateMode.disabled,
91+
super.onReset,
92+
super.focusNode,
93+
this.maxFiles,
94+
this.withData = kIsWeb,
95+
this.withReadStream = false,
96+
this.allowMultiple = false,
97+
this.previewImages = true,
98+
this.typeSelectors = const [
99+
TypeSelector(type: FileType.any, selector: Icon(Icons.add_circle))
100+
],
101+
this.allowedExtensions,
102+
this.onFileLoading,
103+
this.allowCompression = true,
104+
this.customFileViewerBuilder,
105+
this.customTypeViewerBuilder})
106+
: super(
102107
builder: (FormFieldState<List<PlatformFile>?> field) {
103108
final state = field as _FormBuilderFilePickerState;
104109

@@ -109,21 +114,14 @@ class FormBuilderFilePicker
109114
: null),
110115
child: Column(
111116
children: <Widget>[
112-
Row(
113-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
114-
children: <Widget>[
115-
...typeSelectors.map(
116-
(typeSelector) => InkWell(
117-
onTap: state.enabled &&
118-
(null == state._remainingItemCount ||
119-
state._remainingItemCount! > 0)
120-
? () => state.pickFiles(field, typeSelector.type)
121-
: null,
122-
child: typeSelector.selector,
117+
customTypeViewerBuilder != null
118+
? customTypeViewerBuilder(
119+
state.getTypeSelectorActions(typeSelectors, field))
120+
: Row(
121+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
122+
children: state.getTypeSelectorActions(
123+
typeSelectors, field),
123124
),
124-
),
125-
],
126-
),
127125
const SizedBox(height: 3),
128126
customFileViewerBuilder != null
129127
? customFileViewerBuilder.call(state._files,
@@ -305,6 +303,21 @@ class _FormBuilderFilePickerState extends FormBuilderFieldDecorationState<
305303
);
306304
}
307305

306+
List<Widget> getTypeSelectorActions(List<TypeSelector> typeSelectors,
307+
FormFieldState<List<PlatformFile>?> field) {
308+
return <Widget>[
309+
...typeSelectors.map(
310+
(typeSelector) => InkWell(
311+
onTap: enabled &&
312+
(null == _remainingItemCount || _remainingItemCount! > 0)
313+
? () => pickFiles(field, typeSelector.type)
314+
: null,
315+
child: typeSelector.selector,
316+
),
317+
),
318+
];
319+
}
320+
308321
IconData getIconData(String fileExtension) {
309322
final lowerCaseFileExt = fileExtension.toLowerCase();
310323
if (imageFileExts.contains(lowerCaseFileExt)) return Icons.image;

0 commit comments

Comments
 (0)