Sub topic 14
Accessible touch targets and semantics
Accessible interaction means that people can find, understand, and activate controls with different abilities and input methods. A custom control must provide enough space to touch, a useful spoken description, and a clear action.
This chapter assumes that you know Dart closures, widget trees, StatefulWidget, and setState.
Make touch targets large enough
A touch target is the area that responds to a pointer. Material guidance recommends at least 48 by 48 logical pixels for an interactive target. The visible icon can be smaller, but its surrounding hit area should remain large enough.
Use Padding, SizedBox, or the button's built-in constraints to create that space. Do not make a small icon respond only inside its painted bounds.
SizedBox(
width: 48,
height: 48,
child: IconButton(
tooltip: 'Add to favorites',
icon: const Icon(Icons.favorite_border),
onPressed: addFavorite,
),
)IconButton already supplies useful semantics and interaction behavior. Its tooltip describes the action when a user long-presses the button. Screen readers can also announce the tooltip as the control's label.
Describe custom controls with semantics
Flutter's Semantics widget adds information to the accessibility tree. The accessibility tree is a separate representation of the interface that tools such as TalkBack and VoiceOver read aloud.
Set button: true when a widget acts like a button. Set label to identify it and onTap to expose its action. The visual child can still use a GestureDetector for pointer input.
class FavoriteToggle extends StatelessWidget {
const FavoriteToggle({
required this.selected,
required this.onChanged,
super.key,
});
final bool selected;
final ValueChanged<bool> onChanged;
@override
Widget build(BuildContext context) {
final label = selected
? 'Remove from favorites'
: 'Add to favorites';
return Semantics(
button: true,
label: label,
onTap: () => onChanged(!selected),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => onChanged(!selected),
child: SizedBox(
width: 48,
height: 48,
child: Icon(
selected ? Icons.favorite : Icons.favorite_border,
),
),
),
);
}
}The Semantics action and the GestureDetector action call the same callback. This lets both screen-reader users and touch users activate the control.
Avoid exposing duplicate nodes. If a parent semantic node describes the complete control, use ExcludeSemantics around decorative children. A decorative icon usually needs no separate label.
Understand the accessibility tree
Flutter builds a semantics tree from widgets that provide meaningful information. A screen reader traverses this tree rather than interpreting every painted pixel.
Test controls with TalkBack on Android or VoiceOver on iOS. Check that the label describes the result, that the target is easy to reach, and that the action works without relying on color alone.
Exercise
What should you add when a 24 by 24 icon performs an important action?
Show answer
Give it a hit area of at least 48 by 48 logical pixels, add a useful tooltip or semantic label, and expose the action through an appropriate button widget or Semantics.