Skip to content

Commit

Permalink
Made FormBuilderField non-abstract to allow creating custom input fie…
Browse files Browse the repository at this point in the history
…lds (#587)

* Made FormBuilderField non-abstract to allow creating custom input fields
  • Loading branch information
danvick authored Nov 24, 2020
1 parent 7668498 commit ff25d22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
26 changes: 26 additions & 0 deletions example/lib/sources/signup_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ class _SignupFormState extends State<SignupForm> {
]),
),
const SizedBox(height: 10),
FormBuilderField<bool>(
name: 'test',
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(context),
FormBuilderValidators.equal(context, true),
]),
// initialValue: true,
decoration: InputDecoration(labelText: 'Accept Terms?'),
builder: (FormFieldState<bool> field) {
return InputDecorator(
decoration: InputDecoration(
errorText: field.errorText,
),
child: SwitchListTile(
title: Text(
'I have read and accept the terms of service.'),
onChanged: (bool value) {
field.didChange(value);
},
value: field.value ?? false,
),
);
},
),
const SizedBox(height: 10),
MaterialButton(
color: Theme.of(context).accentColor,
child: Text(
Expand All @@ -88,6 +113,7 @@ class _SignupFormState extends State<SignupForm> {
} else {
print('Invalid');
}
print(_formKey.currentState.value);
},
)
],
Expand Down
9 changes: 6 additions & 3 deletions lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum ControlAffinity { leading, trailing }
///
/// This widget maintains the current state of the form field, so that updates
/// and validation errors are visually reflected in the UI.
abstract class FormBuilderField<T> extends FormField<T> {
class FormBuilderField<T> extends FormField<T> {
/// Used to reference the field within the form, or to reference form data
/// after the form is submitted.
final String name;
Expand Down Expand Up @@ -73,11 +73,14 @@ abstract class FormBuilderField<T> extends FormField<T> {
validator: validator,
);

/*@override
FormBuilderFieldState<T> createState();*/
@override
FormFieldState<T> createState();
FormBuilderFieldState<FormBuilderField<T>, T> createState() =>
FormBuilderFieldState<FormBuilderField<T>, T>();
}

abstract class FormBuilderFieldState<F extends FormBuilderField<T>, T>
class FormBuilderFieldState<F extends FormBuilderField<T>, T>
extends FormFieldState<T> {
@override
F get widget => super.widget as F;
Expand Down

0 comments on commit ff25d22

Please sign in to comment.