Sub topic 16
Form Usability and Accessibility
Assume you know basic Dart syntax, nullable types, closures, Flutter widgets, Column, Scaffold, and StatefulWidget. This chapter focuses on making forms understandable and usable for people with different devices, abilities, and input methods.
Give every field a clear purpose
A label tells users what a field means. A hint shows an example. Do not use a hint as the only label because it disappears when the user types.
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
autofillHints: const [AutofillHints.email],
decoration: const InputDecoration(
labelText: 'Email address',
hintText: '[email protected]',
helperText: 'We will send a confirmation link',
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Email address is required';
}
return null;
},
)Flutter uses the label and error text in the field's semantics. Semantics are the information that screen readers expose to users. A useful label should describe the expected value without relying on color or position.
Use visible text for required fields when the distinction matters. For example, write “Phone number (optional)” rather than marking only required fields with an unexplained symbol.
Make validation helpful
Validate at a time that supports the task. Validating every keystroke can make a user see an error before they have finished typing. AutovalidateMode.onUserInteraction is often a reasonable middle ground. It waits for interaction, then updates feedback as the value changes.
Keep messages specific and actionable:
| Weak message | Better message |
|---|---|
| Invalid input | Enter a five-digit ZIP code |
| Error | Passwords must contain at least eight characters |
| Required | Email address is required |
Do not communicate errors only with red borders. Keep the error text visible and ensure the error color has enough contrast. A screen reader can then announce the message, while sighted users receive both color and words.
Support keyboards and touch
Set keyboardType to match the value, and use textInputAction to describe the next action. Move focus in field order, but do not force users to use a particular path. The form must also work with a hardware keyboard, screen reader, switch device, and voice input.
Use FocusNode objects when you need explicit focus movement. Dispose of them in dispose, just like a TextEditingController.
Buttons and checkboxes need comfortable touch areas. Avoid placing tiny controls close together. CheckboxListTile provides a larger combined target than a bare checkbox, and its title gives the control a readable name.
Handle focus and errors
After submission, show validation errors without unexpectedly moving the screen focus. If you move focus, move it to the first invalid field and make that choice consistent. Preserve entered values when rebuilding the form.
A server error should identify the affected field when possible. For example, show “This email is already registered” under the email field. Show a general message near the submit button for errors that do not belong to one field.
Check accessibility in tests
Widget tests can verify that required labels and error messages appear. Also test keyboard submission, focus order, and server-side errors. During manual testing, enlarge text, use a screen reader, navigate without a pointer, and test landscape or narrow screens.
Exercise
Why should a form use both a persistent label and an error message instead of only a hint and a red border?
Show answer
The label keeps the field's purpose clear after typing, and the text error explains the problem to sighted users and screen readers without relying on color.