Sub topic 14
Controlling Reading and Traversal Order
This chapter assumes Basic Dart syntax, Flutter layout widgets, StatelessWidget and StatefulWidget, and the ability to run an app on an emulator or device.
A screen reader reading order and keyboard focus order are related, but they are not the same:
- Reading order controls how assistive technology announces semantic nodes.
- Traversal order controls where keyboard focus moves when a user presses Tab or another navigation key.
- Visual order is the order a sighted user sees on the screen.
These orders usually match when your widget tree matches your layout. Responsive layouts, custom widgets, and complex rows can make them diverge.
Set the reading order
Flutter builds a semantics tree from widgets that describe meaning to accessibility services. A screen reader usually follows the order of semantic nodes in that tree.
Use Semantics.sortKey when the natural order is wrong. OrdinalSortKey assigns an order among semantic siblings. Lower numbers are announced first.
Semantics(
sortKey: const OrdinalSortKey(1),
label: 'Email address',
child: TextField(
decoration: const InputDecoration(labelText: 'Email'),
),
)Place related controls in the same semantic parent when you use sort keys. Sort keys do not automatically reorder unrelated parts of the entire application.
Set keyboard focus order
Keyboard traversal uses focusable widgets. FocusTraversalGroup limits a traversal policy to a part of the widget tree. OrderedTraversalPolicy follows the numeric orders that you provide.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ProfileForm()));
class ProfileForm extends StatelessWidget {
const ProfileForm({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: FocusTraversalGroup(
policy: OrderedTraversalPolicy(),
child: Column(
children: [
FocusTraversalOrder(
order: const NumericFocusOrder(1),
child: TextField(
decoration: const InputDecoration(labelText: 'Name'),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(2),
child: TextField(
decoration: const InputDecoration(labelText: 'Email'),
),
),
FocusTraversalOrder(
order: const NumericFocusOrder(3),
child: FilledButton(
onPressed: () {},
child: const Text('Save'),
),
),
],
),
),
);
}
}The Tab key moves from Name to Email and then to Save, regardless of the default traversal policy.
Keep the orders consistent
A form should normally have the same visual, reading, and focus order. If these differ, users can lose context. For example, a screen reader might announce a Save button before the fields that it submits.
Use the widget tree and layout direction to create a sensible default first. Add sort keys or focus orders only when a real difference requires them. Recheck the result when a layout changes between portrait and landscape modes.
Common errors
- Ordering only visible labels while leaving their controls elsewhere in the traversal sequence.
- Assigning duplicate or confusing numeric sort keys.
- Assuming a visual
Roworder remains correct in right-to-left layouts. - Making a custom control focusable without giving it a useful semantic label.
- Testing only with a mouse or touch.
Exercise
A form visually shows Email, Password, then Submit, but Tab moves from Email to Submit and then Password. What should you change?
Show answer
Put the three controls in one FocusTraversalGroup and assign NumericFocusOrder values 1, 2, and 3 in the intended sequence. Then test the result with a keyboard.