Skip to content

Commit 49f002a

Browse files
committed
Code cleanup: const, final, type safety, removed unnecessary string interpolation.
1 parent ffbff94 commit 49f002a

20 files changed

+88
-96
lines changed

example/lib/home_page.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class HomePage extends StatelessWidget {
1010
Widget build(BuildContext context) {
1111
return Scaffold(
1212
appBar: AppBar(
13-
title: Text('Flutter Form Builder'),
13+
title: const Text('Flutter Form Builder'),
1414
),
1515
body: ListView(
1616
children: <Widget>[
1717
ListTile(
18-
title: Text('Complete Form'),
19-
trailing: Icon(CupertinoIcons.right_chevron),
18+
title: const Text('Complete Form'),
19+
trailing: const Icon(CupertinoIcons.right_chevron),
2020
onTap: () {
2121
return Navigator.of(context).push(
2222
MaterialPageRoute(
@@ -31,10 +31,10 @@ class HomePage extends StatelessWidget {
3131
);
3232
},
3333
),
34-
Divider(),
34+
const Divider(),
3535
ListTile(
36-
title: Text('Signup Form'),
37-
trailing: Icon(CupertinoIcons.right_chevron),
36+
title: const Text('Signup Form'),
37+
trailing: const Icon(CupertinoIcons.right_chevron),
3838
onTap: () {
3939
return Navigator.of(context).push(
4040
MaterialPageRoute(
@@ -49,7 +49,7 @@ class HomePage extends StatelessWidget {
4949
);
5050
},
5151
),
52-
Divider(),
52+
const Divider(),
5353
],
5454
),
5555
);

example/lib/sources/complete_form.dart

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CompleteFormState extends State<CompleteForm> {
2828
@override
2929
Widget build(BuildContext context) {
3030
return Padding(
31-
padding: EdgeInsets.all(10),
31+
padding: const EdgeInsets.all(10),
3232
child: SingleChildScrollView(
3333
child: Column(
3434
children: <Widget>[
@@ -56,7 +56,7 @@ class CompleteFormState extends State<CompleteForm> {
5656
decoration: InputDecoration(labelText: 'Location'),
5757
onChanged: _onChanged,
5858
),
59-
SizedBox(height: 15),
59+
const SizedBox(height: 15),
6060
FormBuilderFilterChip(
6161
name: 'filter_chip',
6262
readOnly: true,
@@ -240,8 +240,8 @@ class CompleteFormState extends State<CompleteForm> {
240240
decoration: InputDecoration(
241241
labelText: 'Age',
242242
suffixIcon: _ageHasError
243-
? Icon(Icons.error, color: Colors.red)
244-
: Icon(Icons.check, color: Colors.green),
243+
? const Icon(Icons.error, color: Colors.red)
244+
: const Icon(Icons.check, color: Colors.green),
245245
),
246246
onChanged: (val) {
247247
setState(() {
@@ -265,8 +265,8 @@ class CompleteFormState extends State<CompleteForm> {
265265
decoration: InputDecoration(
266266
labelText: 'Gender',
267267
suffix: _genderHasError
268-
? Icon(Icons.error)
269-
: Icon(Icons.check),
268+
? const Icon(Icons.error)
269+
: const Icon(Icons.check),
270270
),
271271
// initialValue: 'Male',
272272
allowClear: true,
@@ -276,7 +276,7 @@ class CompleteFormState extends State<CompleteForm> {
276276
items: genderOptions
277277
.map((gender) => DropdownMenuItem(
278278
value: gender,
279-
child: Text('$gender'),
279+
child: Text(gender),
280280
))
281281
.toList(),
282282
onChanged: (val) {
@@ -327,7 +327,7 @@ class CompleteFormState extends State<CompleteForm> {
327327
options: ['Dart', 'Kotlin', 'Java', 'Swift', 'Objective-C']
328328
.map((lang) => FormBuilderFieldOption(
329329
value: lang,
330-
child: Text('$lang'),
330+
child: Text(lang),
331331
))
332332
.toList(growable: false),
333333
controlAffinity: ControlAffinity.trailing,
@@ -343,8 +343,9 @@ class CompleteFormState extends State<CompleteForm> {
343343
.map((number) => FormBuilderFieldOption(
344344
value: number,
345345
child: Text(
346-
'$number',
347-
style: TextStyle(fontWeight: FontWeight.bold),
346+
number.toString(),
347+
style: const TextStyle(
348+
fontWeight: FontWeight.bold),
348349
),
349350
))
350351
.toList(),
@@ -362,8 +363,8 @@ class CompleteFormState extends State<CompleteForm> {
362363
initialValue: 10,
363364
step: 1,
364365
iconSize: 48.0,
365-
addIcon: Icon(Icons.arrow_right),
366-
subtractIcon: Icon(Icons.arrow_left),
366+
addIcon: const Icon(Icons.arrow_right),
367+
subtractIcon: const Icon(Icons.arrow_left),
367368
),
368369
FormBuilderRating(
369370
decoration:
@@ -378,16 +379,16 @@ class CompleteFormState extends State<CompleteForm> {
378379
decoration: const InputDecoration(
379380
labelText: 'The language of my people'),
380381
name: 'languages',
381-
initialValue: ['Dart'],
382-
options: [
382+
initialValue: const ['Dart'],
383+
options: const [
383384
FormBuilderFieldOption(value: 'Dart'),
384385
FormBuilderFieldOption(value: 'Kotlin'),
385386
FormBuilderFieldOption(value: 'Java'),
386387
FormBuilderFieldOption(value: 'Swift'),
387388
FormBuilderFieldOption(value: 'Objective-C'),
388389
],
389390
onChanged: _onChanged,
390-
separator: VerticalDivider(
391+
separator: const VerticalDivider(
391392
width: 10,
392393
thickness: 5,
393394
color: Colors.red,
@@ -407,7 +408,7 @@ class CompleteFormState extends State<CompleteForm> {
407408
decoration: const InputDecoration(labelText: 'Pick Photos'),
408409
maxImages: 1,
409410
),
410-
SizedBox(height: 15),
411+
const SizedBox(height: 15),
411412
FormBuilderPhoneField(
412413
name: 'phone_number',
413414
decoration: const InputDecoration(
@@ -424,13 +425,13 @@ class CompleteFormState extends State<CompleteForm> {
424425
errorText: 'This field required'),
425426
]),
426427
),
427-
SizedBox(height: 15),
428+
const SizedBox(height: 15),
428429
FormBuilderFilePicker(
429430
name: 'files',
430431
previewImages: false,
431432
decoration: InputDecoration(labelText: 'Files filed'),
432433
),
433-
SizedBox(height: 15),
434+
const SizedBox(height: 15),
434435
],
435436
),
436437
),
@@ -439,7 +440,7 @@ class CompleteFormState extends State<CompleteForm> {
439440
Expanded(
440441
child: MaterialButton(
441442
color: Theme.of(context).accentColor,
442-
child: Text(
443+
child: const Text(
443444
'Submit',
444445
style: TextStyle(color: Colors.white),
445446
),
@@ -453,12 +454,13 @@ class CompleteFormState extends State<CompleteForm> {
453454
},
454455
),
455456
),
456-
SizedBox(width: 20),
457+
const SizedBox(width: 20),
457458
Expanded(
458459
child: OutlineButton(
459460
focusNode: FocusNode(),
460461
color: Theme.of(context).accentColor,
461-
child: Text('Reset', style: TextStyle(color: Colors.white)),
462+
child: const Text('Reset',
463+
style: TextStyle(color: Colors.white)),
462464
onPressed: () {
463465
_formKey.currentState.reset();
464466
},

example/lib/sources/signup_form.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class _SignupFormState extends State<SignupForm> {
2727
FormBuilderValidators.required(context),
2828
]),
2929
),
30-
SizedBox(height: 10),
30+
const SizedBox(height: 10),
3131
FormBuilderTextField(
3232
name: 'email',
3333
decoration: InputDecoration(labelText: 'Email'),
@@ -36,7 +36,7 @@ class _SignupFormState extends State<SignupForm> {
3636
FormBuilderValidators.email(context),
3737
]),
3838
),
39-
SizedBox(height: 10),
39+
const SizedBox(height: 10),
4040
FormBuilderTextField(
4141
name: 'password',
4242
decoration: InputDecoration(labelText: 'Password'),
@@ -46,16 +46,17 @@ class _SignupFormState extends State<SignupForm> {
4646
FormBuilderValidators.minLength(context, 6),
4747
]),
4848
),
49-
SizedBox(height: 10),
49+
const SizedBox(height: 10),
5050
FormBuilderTextField(
5151
name: 'confirm_password',
5252
decoration: InputDecoration(
53-
labelText: 'Confirm Password',
54-
suffixIcon: (_formKey.currentState != null &&
55-
!_formKey.currentState.fields['confirm_password']
56-
.isValid)
57-
? Icon(Icons.error, color: Colors.red)
58-
: Icon(Icons.check, color: Colors.green)),
53+
labelText: 'Confirm Password',
54+
suffixIcon: (_formKey.currentState != null &&
55+
!_formKey.currentState.fields['confirm_password']
56+
.isValid)
57+
? const Icon(Icons.error, color: Colors.red)
58+
: const Icon(Icons.check, color: Colors.green),
59+
),
5960
obscureText: true,
6061
validator: FormBuilderValidators.compose([
6162
/*FormBuilderValidators.equal(
@@ -72,7 +73,7 @@ class _SignupFormState extends State<SignupForm> {
7273
}
7374
]),
7475
),
75-
SizedBox(height: 10),
76+
const SizedBox(height: 10),
7677
MaterialButton(
7778
color: Theme.of(context).accentColor,
7879
child: Text(

lib/src/fields/form_builder_choice_chips.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,14 @@ class FormBuilderChoiceChip<T> extends FormBuilderField<T> {
307307
textDirection: textDirection,
308308
verticalDirection: verticalDirection,
309309
children: <Widget>[
310-
for (FormBuilderFieldOption option in options)
310+
for (FormBuilderFieldOption<T> option in options)
311311
ChoiceChip(
312312
label: option.child,
313313
selected: field.value == option.value,
314314
onSelected: state.readOnly
315315
? null
316-
: (bool selected) {
317-
var choice = selected ? option.value : null;
316+
: (selected) {
317+
final choice = selected ? option.value : null;
318318
state.requestFocus();
319319
state.didChange(choice);
320320
},

lib/src/fields/form_builder_color_picker.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class _FormBuilderColorPickerFieldState
215215
if (effectiveFocusNode.hasFocus && !readOnly) {
216216
await Future.microtask(
217217
() => FocusScope.of(context).requestFocus(FocusNode()));
218-
var selected = await showDialog(
218+
final selected = await showDialog<bool>(
219219
context: context,
220220
builder: (BuildContext context) {
221221
final materialLocalizations = MaterialLocalizations.of(context);
@@ -228,21 +228,17 @@ class _FormBuilderColorPickerFieldState
228228
actions: <Widget>[
229229
TextButton(
230230
child: Text(materialLocalizations.cancelButtonLabel),
231-
onPressed: () {
232-
Navigator.of(context).pop(false);
233-
},
231+
onPressed: () => Navigator.pop(context, false),
234232
),
235233
TextButton(
236234
child: Text(materialLocalizations.okButtonLabel),
237-
onPressed: () {
238-
Navigator.of(context).pop(true);
239-
},
235+
onPressed: () => Navigator.pop(context, true),
240236
),
241237
],
242238
);
243239
},
244240
);
245-
if (selected != null && selected == true) {
241+
if (true == selected) {
246242
didChange(_selectedColor);
247243
}
248244
}

lib/src/fields/form_builder_date_range_picker.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ class FormBuilderDateRangePickerState
213213
Future<void> _handleFocus() async {
214214
if (effectiveFocusNode.hasFocus) {
215215
_hideKeyboard();
216-
var initialFirstDate = value?.isEmpty ?? true
216+
final initialFirstDate = value?.isEmpty ?? true
217217
? (widget.initialFirstDate ?? DateTime.now())
218218
: value[0];
219-
var initialLastDate = value?.isEmpty ?? true
219+
final initialLastDate = value?.isEmpty ?? true
220220
? (widget.initialLastDate ?? initialFirstDate)
221221
: (value.length < 2 ? initialFirstDate : value[1]);
222222
final picked = await date_range_picker.showDatePicker(

lib/src/fields/form_builder_dropdown.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class FormBuilderDropdown<T> extends FormBuilderField<T> {
257257
(val) => val.value == field.value,
258258
orElse: () => null)
259259
?.child ??
260-
Text('${field.value.toString()}'))
260+
Text(field.value.toString()))
261261
: disabledHint,
262262
elevation: elevation,
263263
iconSize: iconSize,

lib/src/fields/form_builder_file_picker.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class FormBuilderFilePicker extends FormBuilderField<List<PlatformFile>> {
104104
),
105105
],
106106
),
107-
SizedBox(height: 3),
107+
const SizedBox(height: 3),
108108
state.defaultFileViewer(state._files, field),
109109
],
110110
),
@@ -174,9 +174,10 @@ class _FormBuilderFilePickerState
174174

175175
return LayoutBuilder(
176176
builder: (context, constraints) {
177-
var count = 3;
178-
var spacing = 10;
179-
var itemSize = (constraints.biggest.width - (count * spacing)) / count;
177+
const count = 3;
178+
const spacing = 10;
179+
final itemSize =
180+
(constraints.biggest.width - (count * spacing)) / count;
180181
return Wrap(
181182
// scrollDirection: Axis.horizontal,
182183
alignment: WrapAlignment.start,
@@ -189,7 +190,7 @@ class _FormBuilderFilePickerState
189190
return Container(
190191
height: itemSize,
191192
width: itemSize,
192-
margin: EdgeInsets.only(right: 2),
193+
margin: const EdgeInsets.only(right: 2),
193194
child: Stack(
194195
alignment: Alignment.bottomCenter,
195196
children: <Widget>[
@@ -213,7 +214,7 @@ class _FormBuilderFilePickerState
213214
Container(
214215
padding: const EdgeInsets.symmetric(horizontal: 2),
215216
child: Text(
216-
'${files[index].name}',
217+
files[index].name,
217218
style: theme.textTheme.caption,
218219
maxLines: 2,
219220
overflow: TextOverflow.clip,
@@ -228,15 +229,15 @@ class _FormBuilderFilePickerState
228229
child: InkWell(
229230
onTap: () => removeFileAtIndex(index, field),
230231
child: Container(
231-
margin: EdgeInsets.all(3),
232+
margin: const EdgeInsets.all(3),
232233
decoration: BoxDecoration(
233234
color: Colors.grey.withOpacity(.7),
234235
shape: BoxShape.circle,
235236
),
236237
alignment: Alignment.center,
237238
height: 22,
238239
width: 22,
239-
child: Icon(
240+
child: const Icon(
240241
Icons.close,
241242
size: 18,
242243
color: Colors.white,

lib/src/fields/form_builder_filter_chips.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class FormBuilderFilterChip<T> extends FormBuilderField<List<T>> {
115115
!field.value.contains(option.value))))
116116
? null
117117
: (selected) {
118-
var currentValue = [...field.value];
118+
final currentValue = [...field.value];
119119
if (selected) {
120120
currentValue.add(option.value);
121121
} else {

0 commit comments

Comments
 (0)