79714376

Date: 2025-07-25 07:55:19
Score: 0.5
Natty:
Report link

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);
  }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Dari K