Sub topic 13
Adding checkboxes and other inputs with FormField
Assume you know basic Dart nullability, closures, StatefulWidget, setState, and layouts such as Column and Scaffold. A FormField<T> connects a non-text value to a Form. The type parameter T describes the value being edited, such as bool for a checkbox or String for a selected option.
FormField provides a field state with three important responsibilities:
valuestores the current value.didChange(value)updates the value and notifies the form.hasErroranderrorTextexpose validation results.
Validating a checkbox
A checkbox does not have a built-in validator like TextFormField. Wrap it in FormField<bool> instead.
import 'package:flutter/material.dart';
class PreferencesForm extends StatefulWidget {
const PreferencesForm({super.key});
@override
State<PreferencesForm> createState() => _PreferencesFormState();
}
class _PreferencesFormState extends State<PreferencesForm> {
final _formKey = GlobalKey<FormState>();
void _submit() {
if (_formKey.currentState!.validate()) {
debugPrint('Form is valid');
}
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
FormField<bool>(
initialValue: false,
validator: (value) {
return value == true ? null : 'Accept the terms to continue';
},
builder: (state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CheckboxListTile(
contentPadding: EdgeInsets.zero,
value: state.value ?? false,
onChanged: state.didChange,
title: const Text('I accept the terms'),
),
if (state.hasError)
Padding(
padding: const EdgeInsets.only(left: 12),
child: Text(
state.errorText!,
style: TextStyle(
color: Theme.of(state.context).colorScheme.error,
),
),
),
],
);
},
),
ElevatedButton(
onPressed: _submit,
child: const Text('Continue'),
),
],
),
);
}
}The builder receives the field's state. Passing state.didChange to onChanged keeps the FormField value synchronized with the checkbox. Calling FormState.validate() then runs the checkbox validator with the current value.
FormField<T> works for other controls too. For a dropdown, use FormField<String> and call state.didChange when the selection changes. For a group of radio buttons, use FormField<String> where the value identifies the selected option.
flowchart TD
A[User changes checkbox] --> B[state.didChange]
B --> C[FormField stores the bool value]
C --> D[FormState.validate]
D --> E{Validator result}
E -->|null| F[Form is valid]
E -->|Error string| G[Builder displays error]caption: How a custom form field participates in validation alt: A flowchart showing a checkbox update moving through FormField state and validation to either a valid result or an error message.
Common errors
Do not update only a separate bool variable inside setState. If the FormField does not receive the new value through didChange, the form may validate an old value.
Do not hide the error text unless your custom field provides another clear error presentation. Text fields display errors automatically, but a custom FormField builder must render state.errorText itself.
Exercise
Why must a checkbox callback call state.didChange instead of changing only a separate variable?
Show answer
state.didChange updates the FormField's stored value, so FormState.validate() can validate the current checkbox state.