@@ -113,6 +113,25 @@ class FormBuilderFilePicker extends FormBuilderField<List<PlatformFile>> {
113
113
114
114
class _FormBuilderFilePickerState
115
115
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
+
116
135
List <PlatformFile > _files;
117
136
118
137
int get _remainingItemCount =>
@@ -124,7 +143,7 @@ class _FormBuilderFilePickerState
124
143
super .initState ();
125
144
}
126
145
127
- Future <void > pickFiles (FormFieldState field) async {
146
+ Future <void > pickFiles (FormFieldState < List < PlatformFile >> field) async {
128
147
FilePickerResult resultList;
129
148
130
149
try {
@@ -156,15 +175,16 @@ class _FormBuilderFilePickerState
156
175
}
157
176
}
158
177
159
- void removeFileAtIndex (int index, FormFieldState field) {
178
+ void removeFileAtIndex (int index, FormFieldState < List < PlatformFile >> field) {
160
179
setState (() {
161
180
_files.removeAt (index);
162
181
});
163
182
field.didChange (_files);
164
183
widget.onChanged? .call (_files);
165
184
}
166
185
167
- Widget defaultFileViewer (List <PlatformFile > files, FormFieldState field) {
186
+ Widget defaultFileViewer (
187
+ List <PlatformFile > files, FormFieldState <List <PlatformFile >> field) {
168
188
final theme = Theme .of (context);
169
189
170
190
return LayoutBuilder (
@@ -191,7 +211,7 @@ class _FormBuilderFilePickerState
191
211
children: < Widget > [
192
212
Container (
193
213
alignment: Alignment .center,
194
- child: ([ 'jpg' , 'jpeg' , 'png' ] .contains (
214
+ child: (imageFileExts .contains (
195
215
files[index].extension .toLowerCase ()) &&
196
216
widget.previewImages)
197
217
? Image .file (File (files[index].path),
@@ -251,25 +271,20 @@ class _FormBuilderFilePickerState
251
271
}
252
272
253
273
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;
274
289
}
275
290
}
0 commit comments