Skip to content

Commit 1190501

Browse files
committed
Fixed field registration on form
1 parent f39e891 commit 1190501

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

README.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -494,30 +494,34 @@ RaisedButton(
494494
### Conditional validation
495495
You can also validate a field based on the value of another field
496496
```
497-
FormBuilderRadio(
497+
FormBuilderRadioGroup(
498498
decoration: InputDecoration(labelText: 'My best language'),
499-
attribute: "best_language",
499+
attribute: 'my_language',
500500
validator: FormBuilderValidators.required(context),
501501
options: [
502-
"Dart",
503-
"Kotlin",
504-
"Java",
505-
"Swift",
506-
"Objective-C",
507-
"Other"
502+
'Dart',
503+
'Kotlin',
504+
'Java',
505+
'Swift',
506+
'Objective-C',
507+
'Other'
508508
]
509-
.map((lang) => FormBuilderFieldOption(value: lang))
510-
.toList(growable: false),
511-
),
512-
FormBuilderTextField(
513-
attribute: "specify",
514-
decoration: InputDecoration(labelText: "If Other, please specify"),
515-
validator:
516-
(val){
517-
if(_fbKey.currentState.fields['best_language'].currentState.value == "Other" && (val == null || val.isEmpty))
518-
return "Kindly specify your language";
519-
},
520-
),
509+
.map((lang) => FormBuilderFieldOption(value: lang))
510+
.toList(growable: false),
511+
),
512+
FormBuilderTextField(
513+
attribute: 'specify',
514+
decoration:
515+
InputDecoration(labelText: 'If Other, please specify'),
516+
validator: (val) {
517+
if (_fbKey.currentState.fields['my_language']?.value ==
518+
'Other' &&
519+
(val == null || val.isEmpty)) {
520+
return 'Kindly specify your language';
521+
}
522+
return null;
523+
},
524+
),
521525
```
522526

523527
## CREDITS

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ class FormBuilderDateTimePicker extends FormBuilderField {
224224
final _FormBuilderDateTimePickerState state = field;
225225

226226
return DateTimeField(
227-
key: state.fieldKey,
228227
initialValue: state.initialValue,
229228
format: state.dateFormat,
230229
validator: validator,

lib/src/form_builder.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
23

34
typedef ValueTransformer<T> = dynamic Function(T value);
45

@@ -128,28 +129,28 @@ class FormBuilderState extends State<FormBuilder> {
128129
//TODO: Find way to assert no duplicates in field attributes
129130
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
130131

131-
Map<String, GlobalKey<FormFieldState>> _fieldKeys;
132+
Map<String, FormBuilderFieldState> _fields;
132133

133134
Map<String, dynamic> _value;
134135

135136
Map<String, dynamic> get value => {...widget.initialValue ?? {}, ..._value};
136137

137138
Map<String, dynamic> get initialValue => widget.initialValue;
138139

139-
Map<String, GlobalKey<FormFieldState>> get fields => _fieldKeys;
140+
Map<String, FormBuilderFieldState> get fields => _fields;
140141

141142
bool get readOnly => widget.readOnly;
142143

143144
@override
144145
void initState() {
145146
super.initState();
146-
_fieldKeys = {};
147+
_fields = {};
147148
_value = {};
148149
}
149150

150151
@override
151152
void dispose() {
152-
_fieldKeys = null;
153+
_fields = null;
153154
super.dispose();
154155
}
155156

@@ -159,13 +160,12 @@ class FormBuilderState extends State<FormBuilder> {
159160
});
160161
}
161162

162-
void registerFieldKey(String attribute, GlobalKey<FormFieldState> key) {
163-
// assert(_fieldKeys.containsKey(attribute) == false, "Field with attribute '$attribute' already exists. Make sure that two or more fields don't have the same attribute name.");
164-
_fieldKeys[attribute] = key;
163+
void registerField(String attribute, FormBuilderFieldState field) {
164+
_fields[attribute] = field;
165165
}
166166

167-
void unregisterFieldKey(String attribute) {
168-
_fieldKeys.remove(attribute);
167+
void unregisterField(String attribute) {
168+
_fields.remove(attribute);
169169
}
170170

171171
void save() {

lib/src/form_builder_field.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class FormBuilderField<T> extends FormField<T> {
4040
/// {@macro flutter.widgets.Focus.focusNode}
4141
final FocusNode focusNode;
4242

43+
//TODO: implement bool autofocus, ValueChanged<bool> onValidated
44+
4345
FormBuilderField({
4446
Key key,
4547
//From Super
@@ -85,12 +87,8 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
8587
// Only autovalidate if dirty
8688
bool get autovalidate => dirty && widget.autovalidate;
8789

88-
GlobalKey<FormFieldState> get fieldKey => _fieldKey;
89-
9090
T get initialValue => _initialValue;
9191

92-
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
93-
9492
FormBuilderState _formBuilderState;
9593

9694
bool _readOnly = false;
@@ -110,7 +108,7 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
110108
super.initState();
111109
_formBuilderState = FormBuilder.of(context);
112110
_readOnly = _formBuilderState?.readOnly == true || widget.readOnly;
113-
_formBuilderState?.registerFieldKey(widget.attribute, _fieldKey);
111+
_formBuilderState?.registerField(widget.attribute, this);
114112
_initialValue = widget.initialValue ??
115113
((_formBuilderState?.initialValue?.containsKey(widget.attribute) ??
116114
false)
@@ -127,11 +125,11 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
127125
}
128126

129127
@override
130-
void didChange(T value) {
128+
void didChange(T val) {
131129
setState(() {
132130
_dirty = true;
133131
});
134-
super.didChange(value);
132+
super.didChange(val);
135133
widget.onChanged?.call(value);
136134
}
137135

@@ -153,7 +151,7 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
153151

154152
@override
155153
void dispose() {
156-
_formBuilderState?.unregisterFieldKey(widget.attribute);
154+
_formBuilderState?.unregisterField(widget.attribute);
157155
// The attachment will automatically be detached in dispose().
158156
_focusNode?.dispose();
159157
super.dispose();

0 commit comments

Comments
 (0)