@@ -16,6 +16,7 @@ class FormBuilder extends StatefulWidget {
16
16
final List <FormBuilderInput > controls;
17
17
final bool readonly;
18
18
final bool autovalidate;
19
+ final Key key;
19
20
20
21
const FormBuilder (
21
22
this .context, {
@@ -27,11 +28,9 @@ class FormBuilder extends StatefulWidget {
27
28
this .onWillPop,
28
29
}) : super (key: key);
29
30
30
- final Key key;
31
-
32
31
// assert(duplicateAttributes(controls).length == 0, "Duplicate attribute names not allowed");
33
32
34
- //TODO : Find way to assert no duplicates in control attributes
33
+ //FIXME : Find way to assert no duplicates in control attributes
35
34
/*Function duplicateAttributes = (List<FormBuilderInput> controls) {
36
35
List<String> attributeList = [];
37
36
controls.forEach((c) {
@@ -49,6 +48,7 @@ class FormBuilderState extends State<FormBuilder> {
49
48
final List <FormBuilderInput > formControls;
50
49
final _formKey = GlobalKey <FormState >();
51
50
Map <String , dynamic > value = {};
51
+ Map <String , GlobalKey <FormFieldState >> _fieldKeys = {};
52
52
final _dateTimeFormats = {
53
53
InputType .both: DateFormat ("EEEE, MMMM d, yyyy 'at' h:mma" ),
54
54
InputType .date: DateFormat ('yyyy-MM-dd' ),
@@ -61,6 +61,10 @@ class FormBuilderState extends State<FormBuilder> {
61
61
_formKey.currentState.save ();
62
62
}
63
63
64
+ GlobalKey <FormFieldState > findFieldByAttribute (String attribute){
65
+ return _fieldKeys[attribute];
66
+ }
67
+
64
68
bool validate () {
65
69
return _formKey.currentState.validate ();
66
70
}
@@ -85,7 +89,8 @@ class FormBuilderState extends State<FormBuilder> {
85
89
child: SingleChildScrollView (
86
90
child: Column (
87
91
children: this .formControls.map ((FormBuilderInput formControl) {
88
- // FormBuilderInput formControl = formControl;
92
+ GlobalKey <FormFieldState > _fieldKey = GlobalKey (debugLabel: formControl.attribute);
93
+ _fieldKeys[formControl.attribute] = _fieldKey;
89
94
switch (formControl.type) {
90
95
case FormBuilderInput .TYPE_TEXT :
91
96
case FormBuilderInput .TYPE_PASSWORD :
@@ -116,7 +121,7 @@ class FormBuilderState extends State<FormBuilder> {
116
121
break ;
117
122
}
118
123
return TextFormField (
119
- key: Key (formControl.attribute) ,
124
+ key: _fieldKey ,
120
125
enabled: ! (widget.readonly || formControl.readonly),
121
126
style: (widget.readonly || formControl.readonly)
122
127
? Theme .of (context).textTheme.subhead.copyWith (
@@ -132,7 +137,7 @@ class FormBuilderState extends State<FormBuilder> {
132
137
hintText: formControl.hint,
133
138
helperText: formControl.hint,
134
139
),
135
- autovalidate: formControl.autovalidate,
140
+ autovalidate: formControl.autovalidate ?? false ,
136
141
initialValue:
137
142
formControl.value != null ? "${formControl .value }" : '' ,
138
143
maxLines:
@@ -150,6 +155,10 @@ class FormBuilderState extends State<FormBuilder> {
150
155
? num .tryParse (val)
151
156
: val;
152
157
},
158
+ onFieldSubmitted: (data){
159
+ if (formControl.onChanged != null )
160
+ formControl.onChanged (data);
161
+ },
153
162
validator: (val) {
154
163
if (formControl.require && val.isEmpty)
155
164
return "${formControl .label } is required" ;
@@ -275,7 +284,7 @@ class FormBuilderState extends State<FormBuilder> {
275
284
TextEditingController _typeAheadController =
276
285
TextEditingController (text: formControl.value);
277
286
return TypeAheadFormField (
278
- key: Key (formControl.attribute) ,
287
+ key: _fieldKey ,
279
288
textFieldConfiguration: TextFieldConfiguration (
280
289
enabled: ! (widget.readonly || formControl.readonly),
281
290
controller: _typeAheadController,
@@ -334,7 +343,7 @@ class FormBuilderState extends State<FormBuilder> {
334
343
335
344
case FormBuilderInput .TYPE_DROPDOWN :
336
345
return FormField (
337
- key: Key (formControl.attribute) ,
346
+ key: _fieldKey ,
338
347
enabled: ! (widget.readonly || formControl.readonly),
339
348
initialValue: formControl.value,
340
349
validator: (val) {
@@ -380,7 +389,7 @@ class FormBuilderState extends State<FormBuilder> {
380
389
//TODO: For TYPE_CHECKBOX, TYPE_CHECKBOX_LIST, TYPE_RADIO allow user to choose if checkbox/radio to appear before or after Label
381
390
case FormBuilderInput .TYPE_RADIO :
382
391
return FormField (
383
- key: Key (formControl.attribute) ,
392
+ key: _fieldKey ,
384
393
enabled: ! widget.readonly && ! formControl.readonly,
385
394
initialValue: formControl.value,
386
395
onSaved: (val) {
@@ -442,7 +451,7 @@ class FormBuilderState extends State<FormBuilder> {
442
451
443
452
case FormBuilderInput .TYPE_SEGMENTED_CONTROL :
444
453
return FormField (
445
- key: Key (formControl.attribute) ,
454
+ key: _fieldKey ,
446
455
initialValue: formControl.value,
447
456
enabled: ! (widget.readonly || formControl.readonly),
448
457
onSaved: (val) {
@@ -503,7 +512,7 @@ class FormBuilderState extends State<FormBuilder> {
503
512
504
513
case FormBuilderInput .TYPE_SWITCH :
505
514
return FormField (
506
- key: Key (formControl.attribute) ,
515
+ key: _fieldKey ,
507
516
enabled: ! (widget.readonly || formControl.readonly),
508
517
initialValue: formControl.value ?? false ,
509
518
validator: (value) {
@@ -553,7 +562,7 @@ class FormBuilderState extends State<FormBuilder> {
553
562
case FormBuilderInput .TYPE_STEPPER :
554
563
return FormField (
555
564
enabled: ! (widget.readonly || formControl.readonly),
556
- key: Key (formControl.attribute) ,
565
+ key: _fieldKey ,
557
566
initialValue: formControl.value,
558
567
validator: (value) {
559
568
if (formControl.require && value == null )
@@ -592,7 +601,7 @@ class FormBuilderState extends State<FormBuilder> {
592
601
case FormBuilderInput .TYPE_RATE :
593
602
return FormField (
594
603
enabled: ! (widget.readonly || formControl.readonly),
595
- key: Key (formControl.attribute) ,
604
+ key: _fieldKey ,
596
605
initialValue: formControl.value ?? 1 ,
597
606
validator: (value) {
598
607
if (formControl.require && value == null )
@@ -630,7 +639,7 @@ class FormBuilderState extends State<FormBuilder> {
630
639
631
640
case FormBuilderInput .TYPE_CHECKBOX :
632
641
return FormField (
633
- key: Key (formControl.attribute) ,
642
+ key: _fieldKey ,
634
643
enabled: ! (widget.readonly || formControl.readonly),
635
644
initialValue: formControl.value ?? false ,
636
645
validator: (value) {
@@ -680,7 +689,7 @@ class FormBuilderState extends State<FormBuilder> {
680
689
681
690
case FormBuilderInput .TYPE_SLIDER :
682
691
return FormField (
683
- key: Key (formControl.attribute) ,
692
+ key: _fieldKey ,
684
693
enabled: ! (widget.readonly || formControl.readonly),
685
694
initialValue: formControl.value,
686
695
validator: (value) {
@@ -735,7 +744,7 @@ class FormBuilderState extends State<FormBuilder> {
735
744
736
745
case FormBuilderInput .TYPE_CHECKBOX_LIST :
737
746
return FormField (
738
- key: Key (formControl.attribute) ,
747
+ key: _fieldKey ,
739
748
enabled: ! (widget.readonly || formControl.readonly),
740
749
initialValue: formControl.value ?? [],
741
750
onSaved: (val) {
@@ -808,7 +817,7 @@ class FormBuilderState extends State<FormBuilder> {
808
817
return SizedBox (
809
818
// height: 200.0,
810
819
child: FormField (
811
- key: Key (formControl.attribute) ,
820
+ key: _fieldKey ,
812
821
enabled: ! (widget.readonly || formControl.readonly),
813
822
initialValue: formControl.value ?? [],
814
823
onSaved: (val) {
0 commit comments