Sub topic 3
Exposing swipe and long-press gestures as custom actions
This chapter assumes you know StatelessWidget and StatefulWidget, can build layouts, and can run an app on a device. Screen reader users often cannot perform a swipe or long press, so each gesture needs a named alternative.
Custom semantics actions
A custom semantics action is a named operation that appears in the screen reader's actions menu. Attach it with Semantics:
Semantics(
customSemanticsActions: {
const CustomSemanticsAction(label: 'Delete message'): _deleteMessage,
},
child: Dismissible(
key: ValueKey(message.id),
onDismissed: (_) => _deleteMessage(),
child: MessageTile(message: message),
),
)Announcing changes as they happen
A live region makes the screen reader read new text aloud when it changes, without moving focus.
Semantics(
liveRegion: true,
child: Text('Saved $count items'),
)Respecting reduced motion, bold text and other settings
Read the user's settings from MediaQuery. Use a minimum height instead of a fixed height so large text still fits.
final reduceMotion = MediaQuery.disableAnimationsOf(context);
final boldText = MediaQuery.boldTextOf(context);
final scaler = MediaQuery.textScalerOf(context);
return AnimatedContainer(
duration: reduceMotion ? Duration.zero : const Duration(milliseconds: 300),
constraints: BoxConstraints(minHeight: scaler.scale(48)),
child: Text(
'Continue',
style: TextStyle(fontWeight: boldText ? FontWeight.w700 : FontWeight.w400),
),
);> callout note is not used here.
Keyboard focus and reading order
Focus moves in widget-tree order by default. Use FocusTraversalOrder with OrderedTraversalPolicy to set the sequence explicitly:
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')),
),
],
),
)Screen readers follow the semantics tree, which usually matches layout order. Keep widget order and visual order aligned.
Automated checks in widget tests
Add import 'package:flutter_test/flutter_test.dart'; and run guideline checks in a test:
testWidgets('home page meets accessibility guidelines', (tester) async {
final handle = tester.ensureSemantics();
await tester.pumpWidget(const MaterialApp(home: HomePage()));
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});Accessibility on web and desktop
Keyboard users depend on focus order more on web and desktop. Test Tab, Enter and Escape on every screen. Test web builds with the browser's own screen reader, because the semantics output differs from the mobile platforms.
Exercise
A chat list shows a Dismissible swipe to delete. A screen reader user cannot swipe. Which widget and property expose deletion to them, and what does the user hear?
Show answer
Wrap the item in Semantics and set customSemanticsActions with a CustomSemanticsAction labeled 'Delete message'. The user opens the actions menu and hears "Delete message" as an available action.