formField is already StatefullWidget and if you want to manage state you can create custom state which extends from FormFieldState
class CustomInput extends FormField<bool> {
final Widget label;
final ValueChanged<bool?>? onChanged;
CustomInput({super.key, super.validator, super.initialValue, required this.label, this.onChanged})
: super(
builder: (field) {
final state = field as CustomInputState;
return CheckboxListTile(
value: field.value,
title: label,
onChanged: state.userClicked,
);
},
);
@override
FormFieldState<bool> createState() => CustomInputState();
}
class CustomInputState extends FormFieldState<bool> {
//in the state class you have access to the context and the widget
CustomInput get _widget => widget as CustomInput;
void userClicked(bool? value) {
print('User clicked');
_widget.onChanged?.call(value);
didChange(value);
}
}