Sub topic 13
Keyboard Focus and Navigation
This chapter assumes basic Dart syntax, Flutter layout skills, and familiarity with StatelessWidget and StatefulWidget. It focuses on keyboard users, including people using desktop and web apps, external keyboards on mobile devices, and switch or other assistive input devices.
Why focus matters
Keyboard focus identifies the widget that receives keyboard input. A focused text field receives typed characters. A focused button responds when the user presses Enter or Space. A visible focus indicator shows where the next action will happen.
Many standard Flutter widgets already support focus. TextField, ElevatedButton, Checkbox, and similar controls can receive focus without extra code. Problems appear when:
- The visual order differs from the focus order.
- A custom control cannot receive focus.
- A dialog leaves focus behind its content.
- Code moves focus unexpectedly.
- The focus indicator is hidden or too faint.
A keyboard user should be able to reach every useful control, understand the current location, and move through the page in a logical order.
How Flutter moves focus
Flutter stores focus in a focus tree. The FocusManager tracks the currently focused node, while FocusNode objects represent focusable parts of the interface. The Tab key normally follows Flutter's traversal policy. Arrow keys can move focus in layouts that support directional navigation.
The default policy often follows the widget tree and visual layout. When the intended order is explicit, use FocusTraversalGroup with FocusTraversalOrder.
FocusTraversalGroup(
policy: OrderedTraversalPolicy(),
child: Column(
children: [
FocusTraversalOrder(
order: const NumericFocusOrder(1),
child: TextField(
decoration: const InputDecoration(labelText: 'Email'),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(2),
child: TextField(
decoration: const InputDecoration(labelText: 'Password'),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(3),
child: ElevatedButton(
onPressed: _submit,
child: const Text('Sign in'),
),
),
],
),
)The order values are local to the traversal group. Keep them aligned with the task sequence, not merely with the order in which widgets were created.
Managing focus safely
Use a FocusNode when your code needs to request or observe focus. Create it in State and dispose of it when the widget is removed.
late final FocusNode emailFocus;
@override
void initState() {
super.initState();
emailFocus = FocusNode();
}
@override
void dispose() {
emailFocus.dispose();
super.dispose();
}
void showEmailError() {
emailFocus.requestFocus();
}Request focus only when it supports the user's task, such as moving to the first invalid field after form submission. Avoid requesting focus during every rebuild. That can interrupt typing and cause the screen reader's focus to jump.
Use autofocus sparingly. Automatically focusing a field may open a keyboard or move a desktop user's position before they have chosen an action.
Exercise
Exercise
A form displays the Password field before the Email field visually, but users must enter Email first. How should you fix keyboard navigation?
Show answer
Keep the visual design if needed, then place both fields in a FocusTraversalGroup and assign NumericFocusOrder values so Email comes before Password. Test the sequence with Tab and verify that the focus indicator remains visible.