How can I validate a checkbox in a Flutter Form
? Every other validation works fine, but the checkbox doesn\'t show an Error.
Here is my code:
FormFi
A cleaner solution to this problem is to make a class that extends FormField
Here is how I accomplished this:
class CheckboxFormField extends FormField {
CheckboxFormField(
{Widget title,
FormFieldSetter onSaved,
FormFieldValidator validator,
bool initialValue = false,
bool autovalidate = false})
: super(
onSaved: onSaved,
validator: validator,
initialValue: initialValue,
autovalidate: autovalidate,
builder: (FormFieldState state) {
return CheckboxListTile(
dense: state.hasError,
title: title,
value: state.value,
onChanged: state.didChange,
subtitle: state.hasError
? Builder(
builder: (BuildContext context) => Text(
state.errorText,
style: TextStyle(color: Theme.of(context).errorColor),
),
)
: null,
controlAffinity: ListTileControlAffinity.leading,
);
});
}