Skip to content

Commit 22ffd6c

Browse files
committed
Added fields attribute for FormBuilder to access currentState of fields
1 parent 501c8fc commit 22ffd6c

19 files changed

+436
-5571
lines changed

.idea/libraries/Dart_Packages.xml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 259 additions & 5538 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ class MyHomePageState extends State<MyHomePage> {
266266
attribute: "stepper",
267267
initialValue: 10,
268268
step: 1,
269-
onChanged: _onChanged,
269+
validators: [
270+
(val){
271+
if(!_fbKey.currentState.fields["accept_terms_switch"].currentState.value && val >= 10){
272+
return "You can only put more than 10 if you've accepted terms";
273+
}
274+
}
275+
],
270276
),
271277
FormBuilderRate(
272278
decoration: InputDecoration(labelText: "Rate this form"),

lib/src/fields/form_builder_checkbox.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,25 @@ class FormBuilderCheckbox extends StatefulWidget {
3030

3131
class _FormBuilderCheckboxState extends State<FormBuilderCheckbox> {
3232
bool _readonly = false;
33+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
3334

3435
@override
3536
void initState() {
37+
registerFieldKey();
3638
_readonly =
3739
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
3840
super.initState();
3941
}
4042

43+
registerFieldKey() {
44+
if (FormBuilder.of(context) != null)
45+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
46+
}
47+
4148
@override
4249
Widget build(BuildContext context) {
4350
return FormField(
44-
// key: _fieldKey,
51+
key: _fieldKey,
4552
enabled: !_readonly,
4653
initialValue: widget.initialValue ?? false,
4754
validator: (val) {

lib/src/fields/form_builder_checkbox_list.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,25 @@ class FormBuilderCheckboxList extends StatefulWidget {
3131

3232
class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
3333
bool _readonly = false;
34+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
3435

3536
@override
3637
void initState() {
38+
registerFieldKey();
3739
_readonly =
3840
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
3941
super.initState();
4042
}
4143

44+
registerFieldKey() {
45+
if (FormBuilder.of(context) != null)
46+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
47+
}
48+
4249
@override
4350
Widget build(BuildContext context) {
4451
return FormField(
45-
// key: _fieldKey,
52+
key: _fieldKey,
4653
enabled: !_readonly,
4754
initialValue: widget.initialValue,
4855
validator: (val) {

lib/src/fields/form_builder_chips_input.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,27 @@ class FormBuilderChipsInput<T> extends StatefulWidget {
4040

4141
class _FormBuilderChipsInputState extends State<FormBuilderChipsInput> {
4242
bool _readonly = false;
43+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
4344

4445
@override
4546
void initState() {
47+
registerFieldKey();
4648
_readonly =
4749
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
4850
super.initState();
4951
}
5052

53+
registerFieldKey() {
54+
if (FormBuilder.of(context) != null)
55+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
56+
}
57+
5158
@override
5259
Widget build(BuildContext context) {
5360
return SizedBox(
5461
// height: 200.0,
5562
child: FormField(
56-
// key: _fieldKey,
63+
key: _fieldKey,
5764
enabled: !_readonly,
5865
initialValue: widget.initialValue,
5966
validator: (val) {

lib/src/fields/form_builder_date_time_picker.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,31 @@ class FormBuilderDateTimePicker extends StatefulWidget {
140140

141141
class _FormBuilderDateTimePickerState extends State<FormBuilderDateTimePicker> {
142142
bool _readonly = false;
143+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
144+
145+
final _dateTimeFormats = {
146+
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
147+
InputType.date: DateFormat('yyyy-MM-dd'),
148+
InputType.time: DateFormat("HH:mm"),
149+
};
143150

144151
@override
145152
void initState() {
153+
registerFieldKey();
146154
_readonly =
147155
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
148156
super.initState();
149157
}
150158

151-
final _dateTimeFormats = {
152-
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
153-
InputType.date: DateFormat('yyyy-MM-dd'),
154-
InputType.time: DateFormat("HH:mm"),
155-
};
159+
registerFieldKey() {
160+
if (FormBuilder.of(context) != null)
161+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
162+
}
156163

157164
@override
158165
Widget build(BuildContext context) {
159166
return DateTimePickerFormField(
167+
key: _fieldKey,
160168
inputType: widget.inputType,
161169
initialValue: widget.initialValue,
162170
format: widget.format != null

lib/src/fields/form_builder_dropdown.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ class FormBuilderDropdown extends StatefulWidget {
4444

4545
class _FormBuilderDropdownState extends State<FormBuilderDropdown> {
4646
bool _readonly = false;
47+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
4748

4849
@override
4950
void initState() {
51+
registerFieldKey();
5052
_readonly =
5153
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
5254
super.initState();
5355
}
5456

57+
registerFieldKey() {
58+
if (FormBuilder.of(context) != null)
59+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
60+
}
61+
5562
@override
5663
Widget build(BuildContext context) {
5764
return FormField(
65+
key: _fieldKey,
5866
enabled: !_readonly,
5967
initialValue: widget.initialValue,
6068
validator: (val) {

lib/src/fields/form_builder_radio.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,25 @@ class FormBuilderRadio extends StatefulWidget {
3030

3131
class _FormBuilderRadioState extends State<FormBuilderRadio> {
3232
bool _readonly = false;
33+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
3334

3435
@override
3536
void initState() {
37+
registerFieldKey();
3638
_readonly =
3739
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
3840
super.initState();
3941
}
4042

43+
registerFieldKey() {
44+
if (FormBuilder.of(context) != null)
45+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
46+
}
47+
4148
@override
4249
Widget build(BuildContext context) {
4350
return FormField(
44-
// key: _fieldKey,
51+
key: _fieldKey,
4552
enabled: !_readonly && !_readonly,
4653
initialValue: widget.initialValue,
4754
validator: (val) {

lib/src/fields/form_builder_rate.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@ class FormBuilderRate extends StatefulWidget {
3535

3636
class _FormBuilderRateState extends State<FormBuilderRate> {
3737
bool _readonly = false;
38+
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
3839

3940
@override
4041
void initState() {
42+
registerFieldKey();
4143
_readonly =
4244
(FormBuilder.of(context)?.readonly == true) ? true : widget.readonly;
4345
super.initState();
4446
}
4547

48+
registerFieldKey() {
49+
if (FormBuilder.of(context) != null)
50+
FormBuilder.of(context).registerFieldKey(widget.attribute, _fieldKey);
51+
}
52+
4653
@override
4754
Widget build(BuildContext context) {
4855
return FormField(
56+
key: _fieldKey,
4957
enabled: !_readonly,
50-
// key: _fieldKey,
5158
initialValue: widget.initialValue,
5259
validator: (val) {
5360
for (int i = 0; i < widget.validators.length; i++) {

0 commit comments

Comments
 (0)