Sub topic 16
Accessibility on web and desktop
This chapter assumes you know the semantics tree, the Semantics widget, and Flutter's focus system from the earlier chapters. It covers what changes when your app runs in a browser or on Windows, macOS, or Linux, where users bring screen readers, keyboards, mice, and operating-system settings that differ from those on a phone.
What stays the same
Your semantics tree is platform-independent. Labels, roles, hints, MergeSemantics, and custom actions all work as written. Only the last step differs, which is how each platform's embedder translates the tree into something the local assistive technology understands.
| Platform | Typical screen readers | How semantics reach them |
|---|---|---|
| Web | NVDA, JAWS, VoiceOver, TalkBack in a browser | A hidden DOM overlay with ARIA roles and attributes |
| Windows | Narrator, NVDA, JAWS | The Windows accessibility APIs |
| macOS | VoiceOver | NSAccessibility |
| Linux | Orca | GTK accessibility (AT-SPI) |
Web: semantics are off until requested
A Flutter web app draws everything to a canvas, so the browser sees no buttons, headings, or text. To avoid the cost of building a parallel DOM for every visitor, Flutter leaves the semantics tree disabled at startup. It adds a visually hidden "Enable accessibility" button instead. A screen reader user activates that button, and Flutter then builds the DOM overlay with ARIA roles for each semantics node.
This has two consequences:
- Sighted users and automated page scanners see nothing until semantics are enabled. Tools like Lighthouse report an almost empty page unless you turn semantics on.
- A user must discover and activate the placeholder. You can skip that step by enabling semantics yourself.
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
if (kIsWeb) {
// Keep the returned handle if you want to turn semantics off later.
SemanticsBinding.instance.ensureSemantics();
}
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Hello')))));
}ensureSemantics() returns a SemanticsHandle. Semantics stay on until you call dispose() on every handle you hold.
Enabling semantics permanently costs memory and CPU on every page load, and the cost grows with the number of widgets on screen. Choose deliberately:
- For a public site or an app with legal accessibility requirements, enable semantics at startup.
- For an internal tool where you cannot know in advance whether anyone needs them, leave the default and make sure the placeholder works.
Web details that trip people up
- Text editing. Text fields get a real browser input element in the overlay, so browser autofill, IME input, and screen reader editing behave as expected. Always set
labelTextor aSemanticslabel, or the input has no accessible name. - Selectable text. Static
Textis not selectable by default. Wrap content inSelectionAreaso mouse and keyboard users can select and copy it. Screen reader users also rely on this to inspect text. - Links. A tappable widget that navigates should be marked
Semantics(link: true, ...). Screen readers then announce "link" rather than "button", which sets the right expectation for the user. - Browser zoom. Browser zoom changes the logical size of the viewport, not the text scale setting. Test both. A layout that handles
TextScalercan still break when zoomed to 400% because the viewport becomes very narrow. Use flexible widgets and scrolling instead of fixed widths. - Tab leaving the app. Flutter manages focus inside its own canvas. Pressing Tab past the last focusable widget can move focus to the browser chrome, which is expected. Make sure no keyboard trap exists, meaning a place where the user cannot move focus out or away.
Desktop: keyboard and pointer first
On Windows, macOS, and Linux, the screen reader integration is automatic. Semantics are always on, and the embedder exposes them through the platform API. Your work is mostly about input. Desktop users expect to operate the whole app with the keyboard, and they hover with a mouse.
Standard Material widgets already handle focus, Enter and Space activation, and focus highlights. Custom controls do not. Build custom controls with FocusableActionDetector, which combines focus, hover, shortcuts, and actions in one widget.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(
home: Scaffold(body: Center(child: FavoriteToggle())),
));
class FavoriteToggle extends StatefulWidget {
const FavoriteToggle({super.key});
@override
State<FavoriteToggle> createState() => _FavoriteToggleState();
}
class _FavoriteToggleState extends State<FavoriteToggle> {
bool _on = false;
bool _focused = false;
void _toggle() => setState(() => _on = !_on);
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
toggled: _on,
label: 'Favorite',
child: FocusableActionDetector(
mouseCursor: SystemMouseCursors.click,
onShowFocusHighlight: (value) => setState(() => _focused = value),
actions: {
ActivateIntent: CallbackAction<ActivateIntent>(
onInvoke: (_) {
_toggle();
return null;
},
),
},
child: GestureDetector(
onTap: _toggle,
child: Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(
color: _focused ? Colors.blue : Colors.transparent,
width: 3,
),
borderRadius: BorderRadius.circular(8),
),
child: Icon(_on ? Icons.favorite : Icons.favorite_border),
),
),
),
);
}
}This control does four things a touch-only widget would miss:
- It joins the Tab order, because
FocusableActionDetectorcreates a focus node. - It responds to Enter and Space, because
MaterialAppmaps those keys toActivateIntentby default. - It draws a visible focus ring, driven by
onShowFocusHighlight, which is true only for keyboard-style focus. - It exposes its on/off state to the screen reader through
toggled.
Shortcuts and discoverability
Desktop users expect shortcuts such as Ctrl+S or Cmd+S. Define them with Shortcuts and Actions instead of raw key listeners. Each shortcut then maps to an Intent, and the same action can also be exposed through a menu or button. Remember the platform difference: use SingleActivator(LogicalKeyboardKey.keyS, control: true) on Windows and Linux, and meta: true on macOS. Check defaultTargetPlatform to choose.
Never make a shortcut the only way to reach a feature. Every shortcut needs a visible equivalent, so screen reader users and people who cannot remember key combinations can still find it.
Hover and tooltips
Mouse users get hover feedback, but keyboard users do not hover. Anything revealed on hover, such as a toolbar or a description, must also appear on focus. Material's Tooltip shows on both hover and keyboard focus, and its message becomes the semantics label, so prefer it over a custom hover popup.
Operating system settings on desktop
Desktop operating systems expose the same kinds of preferences as phones. Read them through MediaQuery and design for them:
MediaQuery.highContrastOf(context)reflects a high-contrast request. Provide a theme with stronger borders and colors.MediaQuery.textScalerOf(context)reflects the system text size. On Windows this follows the "Text size" setting, which is separate from display scaling.MediaQuery.disableAnimationsOf(context)reflects a reduced-motion request.
Display scaling (a 150% screen, for example) changes the device pixel ratio, not the text scale. Flutter handles it, but check that your images are sharp at 150% and 200%.
Testing on web and desktop
Automated meetsGuideline checks from the testing chapter run the same way for every target, so keep them in your suite. Add manual checks for what they cannot see:
- Keyboard only. Unplug the mouse. Tab through every screen. Confirm that focus is always visible, that the order matches the reading order, and that you can always leave a dialog with Escape.
- Web screen reader. Open the app in Chrome with NVDA (Windows) or Safari with VoiceOver (macOS). Activate "Enable accessibility" first if you did not force semantics on.
- Browser inspector. Open the accessibility pane in the browser developer tools to read the ARIA tree Flutter generated. Missing names and wrong roles are easy to spot there.
- Desktop screen reader. Run Narrator on Windows, VoiceOver on macOS, or Orca on Linux against a release or profile build.
Common errors
- Assuming a screen reader user on the web will find the hidden placeholder button, without testing the discovery path.
- Building a hover-only menu with no keyboard route.
- Handling
onTapin aGestureDetectorand forgetting focus, so the control cannot be reached with Tab. - Hiding the focus ring for visual polish. Users cannot operate the app without it.
- Defining shortcuts with
control: trueonly, which breaks the macOS convention.
Exercise
A Flutter web app shows a blank page in an accessibility scanner, but a user with NVDA can use it after pressing a hidden button. Why does this happen, and what would you change to make the scanner see the page?
Show answer
Flutter web leaves the semantics tree off until a user activates the hidden "Enable accessibility" placeholder, so the DOM overlay with ARIA nodes does not exist at first. Call SemanticsBinding.instance.ensureSemantics() at startup, guarded by kIsWeb, so semantics are always on. The trade-off is extra memory and CPU on every load.
Exercise
A custom card responds to onTap in a GestureDetector. List three things you must add so desktop keyboard users can operate it.
Show answer
Make it focusable, for example with FocusableActionDetector. Map ActivateIntent to the same callback so Enter and Space work. Show a visible focus indicator using onShowFocusHighlight. Add a Semantics role and label (button: true and a label) so screen readers announce it.