Sub topic 14
Accessible touch targets and semantics
This chapter covers how to make buttons and custom tappable widgets usable by people with motor impairments, low vision, or screen readers. It assumes you know how to build a widget tree with StatelessWidget and StatefulWidget, use setState, lay out widgets with Row, Column, Stack and Padding, and wire up GestureDetector and InkWell. It also assumes you have seen IconButton and its tooltip.
Why accessibility belongs with gestures
A tap is only useful if the user can land it and understand what it does. Two separate problems come up.
- Target size. A finger is far larger than a mouse pointer. A control that is too small causes missed taps and accidental taps on neighbors.
- Semantics. A screen reader such as TalkBack on Android or VoiceOver on iOS does not see pixels. It reads a separate tree of annotated nodes, called the semantics tree. A control that never appears in that tree is invisible to these users.
Flutter's Material buttons handle both problems for you. Custom widgets built from GestureDetector do not, so you must add the same behavior yourself.
Minimum target sizes
Two guidelines are commonly cited.
| Source | Minimum target |
|---|---|
| Material Design | 48 x 48 logical pixels |
| Apple Human Interface Guidelines | 44 x 44 points |
| WCAG 2.2 (level AA, success criterion 2.5.8) | 24 x 24 CSS pixels, with spacing exceptions |
| WCAG 2.1 (level AAA, success criterion 2.5.5) | 44 x 44 CSS pixels |
A safe working rule is 48 x 48 logical pixels for anything tappable. Logical pixels are Flutter's device-independent unit, so 48 of them are about the same physical size on every screen.
The size refers to the area that responds to touch. The visible graphic can be smaller. A 24 pixel icon inside a 48 pixel hit area is fine.
What Material buttons already do
IconButton, TextButton, ElevatedButton, FilledButton and OutlinedButton enforce a minimum tap size through ButtonStyle.minimumSize and tapTargetSize. By default, on mobile platforms, MaterialTapTargetSize.padded expands the touchable area to 48 x 48 even when the painted button is smaller.
You can change this through the theme or per button:
import 'package:flutter/material.dart';
class CompactButtons extends StatelessWidget {
const CompactButtons({super.key});
@override
Widget build(BuildContext context) {
return Row(
children: [
TextButton(
style: TextButton.styleFrom(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
minimumSize: Size.zero,
padding: const EdgeInsets.symmetric(horizontal: 8),
),
onPressed: () {},
child: const Text('Tiny'),
),
TextButton(
onPressed: () {},
child: const Text('Normal'),
),
],
);
}
}The first button is smaller than the guideline because shrinkWrap removes the padding around it. Use this only when another mechanism, such as generous spacing, protects the user. Do not apply shrinkWrap app-wide without a reason.
Enlarging a custom target
A GestureDetector around a small child is only as large as the child. Two approaches fix this.
- Wrap the child in a
ConstrainedBoxwith a minimum size. - Add
Paddinginside the detector, so that the padding counts as part of the hit area.
import 'package:flutter/material.dart';
class SmallDot extends StatelessWidget {
const SmallDot({super.key, required this.onTap});
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 48, minHeight: 48),
child: const Center(
child: SizedBox(
width: 12,
height: 12,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.indigo,
shape: BoxShape.circle,
),
),
),
),
),
);
}
}The visible dot is 12 pixels wide, but the tap area is 48 x 48. HitTestBehavior.opaque matters here. Without it, the empty space around the dot is transparent and the detector ignores touches that land there. You saw this behavior in the hit testing chapter.
Spacing between targets
Size is not enough if two targets touch each other. Leave a gap of about 8 logical pixels between adjacent controls. Two 48 pixel buttons side by side with no gap still invite mistakes.
The semantics tree
Flutter builds the semantics tree alongside the widget tree. Each node can carry a label, a value, a hint, flags such as "is a button" or "is enabled", and actions such as "tap" or "long press". Platform accessibility services read this tree and let the user navigate it with swipes and activate nodes with a double tap.
The next diagram shows how a user action travels from the screen reader to your callback.
Notice that the screen reader never sends a pointer event with coordinates. It sends a semantic action. If your widget has no tap action in its semantics node, a double tap does nothing.
What you get for free
Material widgets add semantics automatically.
ElevatedButton,FilledButton,OutlinedButtonandTextButtonexpose a button role, use the childTextas the label, and report the enabled state. PassingnulltoonPressedmarks the node as disabled, so the screen reader announces it as "dimmed" or "unavailable".IconButtonhas no text, so its label comes fromtooltip. Without a tooltip, a screen reader may announce only "button". That is why the tooltip example in this tutorial matters for more than hover hints.InkWelladds a tap action to the semantics tree whenonTapis set. It does not add a label, so the child needs text or aSemanticswrapper.
What you do not get
GestureDetector does not create a semantics node with a button role. It can contribute a tap action when a matching handler is present, but the node has no label, no button flag and no focus behavior unless the child provides them. A custom control built from GestureDetector and a plain Container is often silent to a screen reader.
Adding semantics with the Semantics widget
Semantics annotates its subtree. The most common properties are listed below.
| Property | Purpose |
|---|---|
| `label` | Text the screen reader speaks for the node |
| `button` | Marks the node as a button |
| `enabled` | Reports whether the control accepts input |
| `onTap` | Registers the action that a double tap triggers |
| `hint` | Describes the result of the action, such as "Opens settings" |
| `excludeSemantics` | Hides the child's own semantics so the label is not read twice |
Here is a complete custom tappable widget that combines a large target, ripple feedback, and semantics.
import 'package:flutter/material.dart';
class AccessibleTile extends StatelessWidget {
const AccessibleTile({
super.key,
required this.label,
required this.icon,
required this.onTap,
this.enabled = true,
});
final String label;
final IconData icon;
final VoidCallback onTap;
final bool enabled;
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
enabled: enabled,
label: label,
excludeSemantics: true,
onTap: enabled ? onTap : null,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: enabled ? onTap : null,
borderRadius: BorderRadius.circular(8),
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 48, minHeight: 48),
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon),
const SizedBox(width: 8),
Text(label),
],
),
),
),
),
),
);
}
}A few points about this code:
excludeSemantics: truereplaces the child semantics with the single node you described. Without it, the screen reader would read the label once fromSemanticsand again from theText.enabledis passed both toSemanticsand toInkWell. Setting only one of them leaves the visual state and the announced state out of step.Materialis present so thatInkWellhas somewhere to paint its ripple.
Merging and excluding
Two related widgets help when you build composite controls.
MergeSemanticscombines the semantics of all descendants into one node. Use it for a row such as aCheckboxnext to aText, so the user hears one item and not two.ExcludeSemanticsremoves a subtree from the semantics tree. Use it for purely decorative content, such as a background illustration.
Overusing excludeSemantics can hide real information. Check that anything you remove is truly redundant.
Tooltips as labels
For icon-only controls, tooltip does three things at once: it shows a hint on hover or long press, it is spoken by screen readers, and it gives the control a name in the semantics tree.
import 'package:flutter/material.dart';
class FavoriteButton extends StatelessWidget {
const FavoriteButton({super.key, required this.isFavorite, required this.onToggle});
final bool isFavorite;
final VoidCallback onToggle;
@override
Widget build(BuildContext context) {
return IconButton(
tooltip: isFavorite ? 'Remove from favorites' : 'Add to favorites',
icon: Icon(isFavorite ? Icons.favorite : Icons.favorite_border),
onPressed: onToggle,
);
}
}Write the tooltip as an action phrase that describes what happens, and update it when the state changes. A tooltip that always says "Favorite" does not tell the user whether they are adding or removing.
Checking your work
You do not need a physical device to catch many problems.
- Turn on a screen reader. Use TalkBack on Android or VoiceOver on iOS and swipe through each screen. Every control should be reachable and should have a meaningful name.
- Show the semantics. Setting
showSemanticsDebugger: trueonMaterialAppdraws the semantics tree over the UI. Controls that are missing there are invisible to assistive tools. - Use the accessibility guideline checks in tests. The
flutter_testpackage ships matchers that verify target size and labels.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('favorite button meets accessibility guidelines', (tester) async {
final handle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: IconButton(
tooltip: 'Add to favorites',
icon: const Icon(Icons.favorite_border),
onPressed: () {},
),
),
),
),
);
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
}androidTapTargetGuideline checks for 48 x 48, iOSTapTargetGuideline checks for 44 x 44, labeledTapTargetGuideline checks that tappable nodes have a label, and textContrastGuideline checks color contrast. ensureSemantics turns the semantics tree on for the test, and you release it with dispose at the end.
Common errors
- Small icon with no padding. A bare
GestureDetectoraround a 16 pixel icon fails the size check. Add aConstrainedBoxor useIconButton. - Transparent area ignores taps. Add
behavior: HitTestBehavior.opaquewhen the hit area is larger than the painted child. - Icon button with no tooltip. The screen reader announces only "button". Add a
tooltip. - Label read twice. A
Semanticslabel plus a childTextproduces duplicate speech. UseexcludeSemantics: trueorMergeSemantics. - Enabled in the UI, disabled in semantics, or the reverse. Drive both from the same boolean.
- Meaning carried by color alone. A red border that signals an error is invisible to color-blind users and screen readers. Add text or an icon, and expose it in the semantics label.
- Text too small to scale. Users can raise the system font size. Avoid fixed-height containers around text, and test with a text scale factor of 1.5 or more.
Summary of decisions
| Situation | Choice |
|---|---|
| Standard action with a text label | A Material button; semantics and size are handled |
| Icon-only action | `IconButton` with a `tooltip` |
| Custom shape or layout needing a tap | `Semantics` + `InkWell` inside `Material`, with a 48 x 48 minimum |
| Several widgets that should be read as one | `MergeSemantics` |
| Decorative graphics | `ExcludeSemantics` |
| Verifying the result | `meetsGuideline` checks and a real screen reader |
Exercise
A teammate builds a close button as GestureDetector(onTap: close, child: Icon(Icons.close, size: 16)). List three accessibility problems with it and say how to fix each.
Show answer
First, the hit area is only 16 x 16, well below the 48 x 48 guideline. Wrap the icon in a ConstrainedBox with minWidth: 48 and minHeight: 48, and set behavior: HitTestBehavior.opaque so the transparent padding also receives taps. Second, the node has no label or button role, so a screen reader cannot name it. Wrap it in Semantics(button: true, label: 'Close', onTap: close, excludeSemantics: true). Third, there is no visual feedback on press. Put the child in an InkWell under a Material ancestor. The simplest fix for all three is to replace the whole thing with IconButton(tooltip: 'Close', icon: const Icon(Icons.close), onPressed: close).
Exercise
You wrap a Row containing an Icon and a Text('Settings') in Semantics(label: 'Settings', button: true, onTap: openSettings, child: ...) without excludeSemantics. What does a screen reader user likely hear, and what do you change?
Show answer
The user is likely to hear "Settings" twice, once from the Semantics label and once from the child Text. Set excludeSemantics: true on the Semantics widget so the child's semantics are dropped and only the single labeled button node remains. Alternatively, use MergeSemantics and let the Text provide the label.
Exercise
Which flutter_test calls do you need to check that a tappable widget is at least 48 x 48 on Android and has a label, and what must you do before and after the checks?
Show answer
Call final handle = tester.ensureSemantics(); before pumping the widget, then use await expectLater(tester, meetsGuideline(androidTapTargetGuideline)); and await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));. Afterward, call handle.dispose(); to release the semantics handle.