Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customise the view of pickers #69

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
95 changes: 54 additions & 41 deletions lib/src/form_builder_file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,39 @@ 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
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,
}) : 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<List<PlatformFile>?> field) {
final state = field as _FormBuilderFilePickerState;

Expand All @@ -109,21 +114,14 @@ class FormBuilderFilePicker
: null),
child: Column(
children: <Widget>[
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,
customTypeViewerBuilder != null
? customTypeViewerBuilder(
state.getTypeSelectorActions(typeSelectors, field))
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: state.getTypeSelectorActions(
typeSelectors, field),
),
),
],
),
const SizedBox(height: 3),
customFileViewerBuilder != null
? customFileViewerBuilder.call(state._files,
Expand Down Expand Up @@ -305,6 +303,21 @@ 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