Skip to content

Commit 3320e9c

Browse files
committed
Expanded the list of supported image file extensions to match Flutter, and added some extra type safety.
1 parent 6288993 commit 3320e9c

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

lib/src/fields/form_builder_file_picker.dart

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ class FormBuilderFilePicker extends FormBuilderField<List<PlatformFile>> {
113113

114114
class _FormBuilderFilePickerState
115115
extends FormBuilderFieldState<FormBuilderFilePicker, List<PlatformFile>> {
116+
/// Image File Extensions.
117+
///
118+
/// Note that images may be previewed.
119+
///
120+
/// This list is inspired by [Image](https://api.flutter.dev/flutter/widgets/Image-class.html)
121+
/// and [instantiateImageCodec](https://api.flutter.dev/flutter/dart-ui/instantiateImageCodec.html):
122+
/// "The following image formats are supported: JPEG, PNG, GIF,
123+
/// Animated GIF, WebP, Animated WebP, BMP, and WBMP."
124+
static const imageFileExts = [
125+
'gif',
126+
'jpg',
127+
'jpeg',
128+
'png',
129+
'webp',
130+
'bmp',
131+
'dib',
132+
'wbmp',
133+
];
134+
116135
List<PlatformFile> _files;
117136

118137
int get _remainingItemCount =>
@@ -124,7 +143,7 @@ class _FormBuilderFilePickerState
124143
super.initState();
125144
}
126145

127-
Future<void> pickFiles(FormFieldState field) async {
146+
Future<void> pickFiles(FormFieldState<List<PlatformFile>> field) async {
128147
FilePickerResult resultList;
129148

130149
try {
@@ -156,15 +175,16 @@ class _FormBuilderFilePickerState
156175
}
157176
}
158177

159-
void removeFileAtIndex(int index, FormFieldState field) {
178+
void removeFileAtIndex(int index, FormFieldState<List<PlatformFile>> field) {
160179
setState(() {
161180
_files.removeAt(index);
162181
});
163182
field.didChange(_files);
164183
widget.onChanged?.call(_files);
165184
}
166185

167-
Widget defaultFileViewer(List<PlatformFile> files, FormFieldState field) {
186+
Widget defaultFileViewer(
187+
List<PlatformFile> files, FormFieldState<List<PlatformFile>> field) {
168188
final theme = Theme.of(context);
169189

170190
return LayoutBuilder(
@@ -191,7 +211,7 @@ class _FormBuilderFilePickerState
191211
children: <Widget>[
192212
Container(
193213
alignment: Alignment.center,
194-
child: (['jpg', 'jpeg', 'png'].contains(
214+
child: (imageFileExts.contains(
195215
files[index].extension.toLowerCase()) &&
196216
widget.previewImages)
197217
? Image.file(File(files[index].path),
@@ -251,25 +271,20 @@ class _FormBuilderFilePickerState
251271
}
252272

253273
IconData getIconData(String fileExtension) {
254-
switch (fileExtension.toLowerCase()) {
255-
case 'jpg':
256-
case 'jpeg':
257-
case 'png':
258-
case 'gif':
259-
return Icons.image;
260-
case 'xls':
261-
case 'xlsx':
262-
return CommunityMaterialIcons.file_excel;
263-
case 'doc':
264-
case 'docx':
265-
return CommunityMaterialIcons.file_word;
266-
case 'pdf':
267-
return CommunityMaterialIcons.file_pdf;
268-
case 'txt':
269-
case 'log':
270-
return CommunityMaterialIcons.script_text;
271-
default:
272-
return Icons.insert_drive_file;
273-
}
274+
// Check if the file is an image first (because there is a shared variable
275+
// with preview logic), and then fallback to non-image file ext lookup.
276+
const nonImageFileExtIcons = {
277+
'doc': CommunityMaterialIcons.file_word,
278+
'docx': CommunityMaterialIcons.file_word,
279+
'log': CommunityMaterialIcons.script_text,
280+
'pdf': CommunityMaterialIcons.file_pdf,
281+
'txt': CommunityMaterialIcons.script_text,
282+
'xls': CommunityMaterialIcons.file_excel,
283+
'xlsx': CommunityMaterialIcons.file_excel,
284+
};
285+
final lowerCaseFileExt = fileExtension.toLowerCase();
286+
return imageFileExts.contains(lowerCaseFileExt)
287+
? Icons.image
288+
: nonImageFileExtIcons[lowerCaseFileExt] ?? Icons.insert_drive_file;
274289
}
275290
}

0 commit comments

Comments
 (0)