Skip to content

Commit 288727d

Browse files
authored
Revert "Refactor to support a common FormBuilderField class. (#516)" (#550)
This reverts commit fbaf3bc.
1 parent dba6046 commit 288727d

30 files changed

+1693
-1128
lines changed

example/lib/main.dart

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class MyHomePageState extends State<MyHomePage> {
378378
}
379379
},
380380
),
381-
const SizedBox(height: 15),
381+
SizedBox(height: 15),
382382
FormBuilderRadioGroup(
383383
orientation: GroupedRadioOrientation.wrap,
384384
decoration:
@@ -400,13 +400,13 @@ class MyHomePageState extends State<MyHomePage> {
400400
))
401401
.toList(growable: false),
402402
),
403-
const SizedBox(height: 15),
404-
const SizedBox(height: 15),
403+
SizedBox(height: 15),
404+
SizedBox(height: 15),
405405
FormBuilderSegmentedControl(
406-
decoration: const InputDecoration(
407-
labelText: 'Movie Rating (Archer)'),
406+
decoration:
407+
InputDecoration(labelText: 'Movie Rating (Archer)'),
408408
attribute: 'movie_rating',
409-
textStyle: const TextStyle(fontWeight: FontWeight.bold),
409+
textStyle: TextStyle(fontWeight: FontWeight.bold),
410410
options: List.generate(5, (i) => i + 1)
411411
.map((number) => FormBuilderFieldOption(
412412
value: number,
@@ -418,7 +418,7 @@ class MyHomePageState extends State<MyHomePage> {
418418
.toList(),
419419
onChanged: _onChanged,
420420
),
421-
const SizedBox(height: 15),
421+
SizedBox(height: 15),
422422
FormBuilderSwitch(
423423
label: Text('I Accept the tems and conditions'),
424424
attribute: 'accept_terms_switch',
@@ -451,11 +451,11 @@ class MyHomePageState extends State<MyHomePage> {
451451
),
452452
SizedBox(height: 15),
453453
FormBuilderCheckboxGroup(
454-
decoration: const InputDecoration(
455-
labelText: 'The language of my people'),
454+
decoration:
455+
InputDecoration(labelText: 'The language of my people'),
456456
attribute: 'languages',
457457
initialValue: ['Dart'],
458-
options: const [
458+
options: [
459459
FormBuilderFieldOption(value: 'Dart'),
460460
FormBuilderFieldOption(value: 'Kotlin'),
461461
FormBuilderFieldOption(value: 'Java'),
@@ -464,7 +464,7 @@ class MyHomePageState extends State<MyHomePage> {
464464
],
465465
onChanged: _onChanged,
466466
),
467-
const SizedBox(height: 15),
467+
SizedBox(height: 15),
468468
FormBuilderImagePicker(
469469
attribute: 'images',
470470
decoration: const InputDecoration(
@@ -540,34 +540,38 @@ class MyHomePageState extends State<MyHomePage> {
540540
],
541541
),
542542
SizedBox(height: 15),
543-
FormBuilderCustomField<String>(
543+
FormBuilderCustomField(
544544
attribute: 'name',
545545
validators: [FormBuilderValidators.required()],
546546
initialValue: 'Argentina',
547-
builder: (FormFieldState<String> field) {
548-
return InputDecorator(
549-
decoration: InputDecoration(
550-
labelText: 'FormBuilderCustomField',
551-
contentPadding: const EdgeInsets.only(
552-
top: 10.0,
553-
bottom: 0.0,
547+
formField: FormField(
548+
enabled: true,
549+
builder: (FormFieldState<dynamic> field) {
550+
return InputDecorator(
551+
decoration: InputDecoration(
552+
labelText: 'FormBuilderCustomField',
553+
contentPadding: const EdgeInsets.only(
554+
top: 10.0,
555+
bottom: 0.0,
556+
),
557+
border: InputBorder.none,
558+
errorText: field.errorText,
554559
),
555-
border: InputBorder.none,
556-
errorText: field.errorText,
557-
),
558-
child: Container(
559-
height: 200,
560-
child: CupertinoPicker(
561-
itemExtent: 30,
562-
children: allCountries.map((c) => Text(c)).toList(),
563-
onSelectedItemChanged: (index) {
564-
print(index);
565-
field.didChange(allCountries[index]);
566-
},
560+
child: Container(
561+
height: 200,
562+
child: CupertinoPicker(
563+
itemExtent: 30,
564+
children:
565+
allCountries.map((c) => Text(c)).toList(),
566+
onSelectedItemChanged: (index) {
567+
print(index);
568+
field.didChange(allCountries[index]);
569+
},
570+
),
567571
),
568-
),
569-
);
570-
},
572+
);
573+
},
574+
),
571575
),
572576
],
573577
),

lib/src/fields/form_builder_checkbox.dart

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import 'package:flutter/rendering.dart';
33
import 'package:flutter/widgets.dart';
44
import 'package:flutter_form_builder/flutter_form_builder.dart';
55

6-
class FormBuilderCheckbox extends FormBuilderField<bool> {
6+
class FormBuilderCheckbox extends StatefulWidget {
7+
final String attribute;
8+
final List<FormFieldValidator> validators;
9+
final bool initialValue;
10+
final bool readOnly;
11+
final InputDecoration decoration;
12+
final ValueChanged onChanged;
13+
final ValueTransformer valueTransformer;
714
final bool leadingInput;
815

916
final Widget label;
@@ -12,6 +19,7 @@ class FormBuilderCheckbox extends FormBuilderField<bool> {
1219
final Color checkColor;
1320
final MaterialTapTargetSize materialTapTargetSize;
1421
final bool tristate;
22+
final FormFieldSetter onSaved;
1523
final EdgeInsets contentPadding;
1624
final Color focusColor;
1725
final Color hoverColor;
@@ -22,57 +30,64 @@ class FormBuilderCheckbox extends FormBuilderField<bool> {
2230

2331
FormBuilderCheckbox({
2432
Key key,
25-
@required String attribute,
26-
bool readOnly = false,
27-
AutovalidateMode autovalidateMode,
28-
bool enabled = true,
29-
bool initialValue,
30-
InputDecoration decoration = const InputDecoration(),
31-
ValueChanged<bool> onChanged,
32-
FormFieldSetter<bool> onSaved,
33-
ValueTransformer<bool> valueTransformer,
34-
List<FormFieldValidator<bool>> validators = const [],
33+
@required this.attribute,
3534
@required this.label,
35+
this.initialValue,
36+
this.validators = const [],
37+
this.readOnly = false,
38+
this.decoration = const InputDecoration(),
39+
this.onChanged,
40+
this.valueTransformer,
3641
this.leadingInput = false,
3742
this.activeColor,
3843
this.checkColor,
3944
this.materialTapTargetSize,
4045
this.tristate = false,
41-
this.contentPadding = EdgeInsets.zero,
46+
this.onSaved,
47+
this.contentPadding = const EdgeInsets.all(0.0),
4248
this.focusColor,
4349
this.hoverColor,
4450
this.focusNode,
4551
this.autoFocus = false,
4652
this.mouseCursor,
4753
this.visualDensity,
48-
}) : super(
49-
key: key,
50-
attribute: attribute,
51-
readOnly: readOnly,
52-
autovalidateMode: autovalidateMode,
53-
enabled: enabled,
54-
initialValue: initialValue,
55-
decoration: decoration,
56-
onChanged: onChanged,
57-
onSaved: onSaved,
58-
valueTransformer: valueTransformer,
59-
validators: validators,
60-
);
54+
}) : super(key: key);
6155

6256
@override
6357
_FormBuilderCheckboxState createState() => _FormBuilderCheckboxState();
6458
}
6559

66-
class _FormBuilderCheckboxState
67-
extends FormBuilderFieldState<FormBuilderCheckbox, bool, bool> {
68-
Widget _checkbox(FormFieldState<bool> field) {
60+
class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
61+
bool _readOnly = false;
62+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
63+
FormBuilderState _formState;
64+
bool _initialValue;
65+
66+
@override
67+
void initState() {
68+
_formState = FormBuilder.of(context);
69+
_formState?.registerFieldKey(widget.attribute, _fieldKey);
70+
_initialValue = widget.initialValue ??
71+
((_formState?.initialValue?.containsKey(widget.attribute) ?? false)
72+
? _formState.initialValue[widget.attribute]
73+
: null);
74+
super.initState();
75+
}
76+
77+
@override
78+
void dispose() {
79+
_formState?.unregisterFieldKey(widget.attribute);
80+
super.dispose();
81+
}
82+
83+
Widget _checkbox(FormFieldState<dynamic> field) {
6984
return Checkbox(
7085
value: (field.value == null && !widget.tristate) ? false : field.value,
7186
activeColor: widget.activeColor,
7287
checkColor: widget.checkColor,
7388
materialTapTargetSize: widget.materialTapTargetSize,
7489
tristate: widget.tristate,
75-
onChanged: readOnly
90+
onChanged: _readOnly
7691
? null
7792
: (bool value) {
7893
FocusScope.of(context).requestFocus(FocusNode());
@@ -88,29 +103,40 @@ class _FormBuilderCheckboxState
88103
);
89104
}
90105

91-
Widget _leading(FormFieldState<bool> field) {
106+
Widget _leading(FormFieldState<dynamic> field) {
92107
if (widget.leadingInput) return _checkbox(field);
93108
return null;
94109
}
95110

96-
Widget _trailing(FormFieldState<bool> field) {
111+
Widget _trailing(FormFieldState<dynamic> field) {
97112
if (!widget.leadingInput) return _checkbox(field);
98113
return null;
99114
}
100115

101116
@override
102117
Widget build(BuildContext context) {
103-
return FormField<bool>(
104-
key: fieldKey,
105-
enabled: widget.enabled,
106-
initialValue: initialValue,
107-
autovalidateMode: widget.autovalidateMode,
108-
validator: (val) => validate(val),
109-
onSaved: (val) => save(val),
110-
builder: (FormFieldState<bool> field) {
118+
_readOnly = _formState?.readOnly == true || widget.readOnly;
119+
120+
return FormField(
121+
key: _fieldKey,
122+
enabled: !_readOnly,
123+
initialValue: _initialValue,
124+
validator: (val) =>
125+
FormBuilderValidators.validateValidators(val, widget.validators),
126+
onSaved: (val) {
127+
var transformed;
128+
if (widget.valueTransformer != null) {
129+
transformed = widget.valueTransformer(val);
130+
_formState?.setAttributeValue(widget.attribute, transformed);
131+
} else {
132+
_formState?.setAttributeValue(widget.attribute, val);
133+
}
134+
widget.onSaved?.call(transformed ?? val);
135+
},
136+
builder: (FormFieldState<dynamic> field) {
111137
return InputDecorator(
112138
decoration: widget.decoration.copyWith(
113-
enabled: widget.enabled,
139+
enabled: !_readOnly,
114140
errorText: field.errorText,
115141
),
116142
child: ListTile(
@@ -120,7 +146,7 @@ class _FormBuilderCheckboxState
120146
title: widget.label,
121147
leading: _leading(field),
122148
trailing: _trailing(field),
123-
onTap: readOnly
149+
onTap: _readOnly
124150
? null
125151
: () {
126152
FocusScope.of(context).requestFocus(FocusNode());

0 commit comments

Comments
 (0)