Skip to content

Commit ff25d22

Browse files
authored
Made FormBuilderField non-abstract to allow creating custom input fields (#587)
* Made FormBuilderField non-abstract to allow creating custom input fields
1 parent 7668498 commit ff25d22

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

example/lib/sources/signup_form.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,31 @@ class _SignupFormState extends State<SignupForm> {
7676
]),
7777
),
7878
const SizedBox(height: 10),
79+
FormBuilderField<bool>(
80+
name: 'test',
81+
validator: FormBuilderValidators.compose([
82+
FormBuilderValidators.required(context),
83+
FormBuilderValidators.equal(context, true),
84+
]),
85+
// initialValue: true,
86+
decoration: InputDecoration(labelText: 'Accept Terms?'),
87+
builder: (FormFieldState<bool> field) {
88+
return InputDecorator(
89+
decoration: InputDecoration(
90+
errorText: field.errorText,
91+
),
92+
child: SwitchListTile(
93+
title: Text(
94+
'I have read and accept the terms of service.'),
95+
onChanged: (bool value) {
96+
field.didChange(value);
97+
},
98+
value: field.value ?? false,
99+
),
100+
);
101+
},
102+
),
103+
const SizedBox(height: 10),
79104
MaterialButton(
80105
color: Theme.of(context).accentColor,
81106
child: Text(
@@ -88,6 +113,7 @@ class _SignupFormState extends State<SignupForm> {
88113
} else {
89114
print('Invalid');
90115
}
116+
print(_formKey.currentState.value);
91117
},
92118
)
93119
],

lib/src/form_builder_field.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ enum ControlAffinity { leading, trailing }
99
///
1010
/// This widget maintains the current state of the form field, so that updates
1111
/// and validation errors are visually reflected in the UI.
12-
abstract class FormBuilderField<T> extends FormField<T> {
12+
class FormBuilderField<T> extends FormField<T> {
1313
/// Used to reference the field within the form, or to reference form data
1414
/// after the form is submitted.
1515
final String name;
@@ -73,11 +73,14 @@ abstract class FormBuilderField<T> extends FormField<T> {
7373
validator: validator,
7474
);
7575

76+
/*@override
77+
FormBuilderFieldState<T> createState();*/
7678
@override
77-
FormFieldState<T> createState();
79+
FormBuilderFieldState<FormBuilderField<T>, T> createState() =>
80+
FormBuilderFieldState<FormBuilderField<T>, T>();
7881
}
7982

80-
abstract class FormBuilderFieldState<F extends FormBuilderField<T>, T>
83+
class FormBuilderFieldState<F extends FormBuilderField<T>, T>
8184
extends FormFieldState<T> {
8285
@override
8386
F get widget => super.widget as F;

0 commit comments

Comments
 (0)