A user types into a text field. Their onChanged programmatically toggles a checkbox. The text field loses focus mid-keystroke. The keyboard might dismiss on mobile. The user's cursor is gone and they don't know why.
That's issue #1495 in flutter_form_builder, a package with 1,900+ likes on pub.dev. The checkbox was stealing focus every time its value changed, even when no human touched it.
Root cause
_FormBuilderCheckboxState overrides didChange to call focus() before delegating to super.didChange(value). Every programmatic value change (not just taps) triggers didChange. So any code path that sets the checkbox value also yanks focus to the checkbox.
@override
void didChange(bool? value) {
focus(); // <-- this fires on EVERY value change, not just user taps
super.didChange(value);
}
The focus() call was probably added for keyboard accessibility on direct taps. But CheckboxListTile already handles tap-based focus through its focusNode parameter. The override was redundant for taps and destructive for programmatic changes.
The fix
Delete the override. Seven lines removed, zero added to the widget itself. FormBuilderSwitch already works this way (no didChange override), so this brings checkbox in line with its sibling.
The test I added reproduces the exact scenario from the issue: a FormBuilderTextField whose onChanged calls didChange(true) on a checkbox, then asserts the text field still has focus. 57 lines added total across the widget file and test file. All 149 existing tests pass, flutter analyze clean.
Merged by deandreamatias (one of the package maintainers). Codecov confirmed full coverage on the changed paths.
Why small patches matter
This is a 7-line deletion with a 50-line test. The PR took about 40 minutes to write including the regression test. But the bug affects anyone who programmatically controls a FormBuilderCheckbox from another field's callback. That's a common pattern in multi-step forms: "check the terms box when the user fills the last required field," "toggle a preference when a related dropdown changes." Every one of those integrations would hit this.
The fix for FormBuilderSwitch was already in place. Nobody noticed that FormBuilderCheckbox diverged. That's how these bugs survive in form libraries: each field type is a separate class, the override exists in one but not the other, and the behavioral difference only surfaces when you wire fields together.
Top comments (0)