Skip to content

Commit 2992bf1

Browse files
committed
Some code improvements
1 parent 90cd13f commit 2992bf1

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:pedantic/analysis_options.yaml

example/lib/main.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class MyHomePage extends StatefulWidget {
2323
}
2424

2525
class _MyHomePageState extends State<MyHomePage> {
26-
GlobalKey<FormBuilderState> _formKey = GlobalKey();
26+
final _formKey = GlobalKey<FormBuilderState>();
2727

2828
@override
2929
Widget build(BuildContext context) {
3030
return Scaffold(
3131
appBar: AppBar(
32-
title: Text("FormBuilder FilePicker Example"),
32+
title: Text('FormBuilder FilePicker Example'),
3333
),
3434
body: Padding(
3535
padding: EdgeInsets.all(10),
@@ -38,8 +38,8 @@ class _MyHomePageState extends State<MyHomePage> {
3838
child: Column(
3939
children: <Widget>[
4040
FormBuilderFilePicker(
41-
name: "images",
42-
decoration: InputDecoration(labelText: "Attachments"),
41+
name: 'images',
42+
decoration: InputDecoration(labelText: 'Attachments'),
4343
maxFiles: null,
4444
previewImages: true,
4545
onChanged: (val) => print(val),
@@ -54,7 +54,7 @@ class _MyHomePageState extends State<MyHomePage> {
5454
},
5555
),
5656
SizedBox(height: 20),
57-
RaisedButton(
57+
ElevatedButton(
5858
child: Text('Submit'),
5959
onPressed: () {
6060
_formKey.currentState.save();

lib/src/form_builder_file_picker.dart

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ class FormBuilderFilePicker extends FormBuilderField<List<PlatformFile>> {
9797
if (maxFiles != null)
9898
Text('${state._files.length} / $maxFiles'),
9999
InkWell(
100-
child: selector,
101100
onTap: state.enabled &&
102101
(null == state._remainingItemCount ||
103102
state._remainingItemCount > 0)
104103
? () => state.pickFiles(field)
105104
: null,
105+
child: selector,
106106
),
107107
],
108108
),
@@ -226,24 +226,24 @@ class _FormBuilderFilePickerState
226226
fit: BoxFit.cover)
227227
: Container(
228228
alignment: Alignment.center,
229+
color: theme.primaryColor,
229230
child: Icon(
230231
getIconData(files[index].extension),
231232
color: Colors.white,
232233
size: 56,
233234
),
234-
color: theme.primaryColor,
235235
),
236236
),
237237
Container(
238238
padding: const EdgeInsets.symmetric(horizontal: 2),
239+
width: double.infinity,
240+
color: Colors.white.withOpacity(.8),
239241
child: Text(
240242
files[index].name,
241243
style: theme.textTheme.caption,
242244
maxLines: 2,
243245
overflow: TextOverflow.clip,
244246
),
245-
width: double.infinity,
246-
color: Colors.white.withOpacity(.8),
247247
),
248248
if (enabled)
249249
Positioned(
@@ -279,20 +279,25 @@ class _FormBuilderFilePickerState
279279
}
280280

281281
IconData getIconData(String fileExtension) {
282+
final lowerCaseFileExt = fileExtension.toLowerCase();
283+
if (imageFileExts.contains(lowerCaseFileExt)) return Icons.image;
282284
// Check if the file is an image first (because there is a shared variable
283285
// with preview logic), and then fallback to non-image file ext lookup.
284-
const nonImageFileExtIcons = {
285-
'doc': CommunityMaterialIcons.file_word,
286-
'docx': CommunityMaterialIcons.file_word,
287-
'log': CommunityMaterialIcons.script_text,
288-
'pdf': CommunityMaterialIcons.file_pdf,
289-
'txt': CommunityMaterialIcons.script_text,
290-
'xls': CommunityMaterialIcons.file_excel,
291-
'xlsx': CommunityMaterialIcons.file_excel,
292-
};
293-
final lowerCaseFileExt = fileExtension.toLowerCase();
294-
return imageFileExts.contains(lowerCaseFileExt)
295-
? Icons.image
296-
: nonImageFileExtIcons[lowerCaseFileExt] ?? Icons.insert_drive_file;
286+
switch (lowerCaseFileExt) {
287+
case 'doc':
288+
case 'docx':
289+
return CommunityMaterialIcons.file_word;
290+
case 'log':
291+
return CommunityMaterialIcons.script_text;
292+
case 'pdf':
293+
return CommunityMaterialIcons.file_pdf;
294+
case 'txt':
295+
return CommunityMaterialIcons.script_text;
296+
case 'xls':
297+
case 'xlsx':
298+
return CommunityMaterialIcons.file_excel;
299+
default:
300+
return Icons.insert_drive_file;
301+
}
297302
}
298303
}

0 commit comments

Comments
 (0)