Skip to content

Commit

Permalink
[FEATURE] Allow to customise the view of pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
lowminchien committed Sep 3, 2023
1 parent eee7332 commit 21a0007
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class MyHomePageState extends State<MyHomePage> {
),
)
],
customTypeViewerBuilder: (children) => Row(
mainAxisAlignment: MainAxisAlignment.end,
children: children,
),
onFileLoading: (val) {
debugPrint(val.toString());
},
Expand Down
37 changes: 24 additions & 13 deletions lib/src/form_builder_file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Widget> types)? customTypeViewerBuilder;

/// Creates field for image(s) from user device storage
FormBuilderFilePicker({
//From Super
Expand Down Expand Up @@ -98,6 +101,7 @@ class FormBuilderFilePicker
this.onFileLoading,
this.allowCompression = true,
this.customFileViewerBuilder,
this.customTypeViewerBuilder
}) : super(
builder: (FormFieldState<List<PlatformFile>?> field) {
final state = field as _FormBuilderFilePickerState;
Expand All @@ -109,20 +113,11 @@ class FormBuilderFilePicker
: null),
child: Column(
children: <Widget>[
Row(
customTypeViewerBuilder != null
? customTypeViewerBuilder(state.getTypeSelectorActions(typeSelectors, field))
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
...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
Expand Down Expand Up @@ -305,6 +300,22 @@ class _FormBuilderFilePickerState extends FormBuilderFieldDecorationState<
);
}

List<Widget> getTypeSelectorActions(List<TypeSelector> typeSelectors, FormFieldState<List<PlatformFile>?> field) {
return <Widget>[
...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;
Expand Down

0 comments on commit 21a0007

Please sign in to comment.