Sub topic 7
Making Tap Targets Large Enough
This chapter assumes that you can build Flutter layouts and use StatelessWidget and StatefulWidget. A tap target is the area that responds to a pointer or touch event. The visible icon or text inside it can be smaller than the target.
A larger target helps people with limited dexterity, tremors, or one-handed use. It also reduces accidental taps. A common minimum is 48 by 48 logical pixels for Android and Flutter Material interfaces. iOS commonly recommends 44 by 44 points. Treat these values as minimums, not ideal sizes for every control.
Separate the control from its visible content
An icon might be only 24 pixels wide, but its button should usually provide extra space around it. Standard widgets such as IconButton, Checkbox, and FilledButton already provide platform-aware interaction behavior and accessibility semantics.
For a custom control, give the interactive widget a minimum size. Do not rely only on the size of its child:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: TapTargetDemo()));
}
class TapTargetDemo extends StatelessWidget {
const TapTargetDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Tap target')),
body: Center(
child: Semantics(
button: true,
label: 'Add to favorites',
hint: 'Saves this item to your list',
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
// Save the item.
},
child: const ConstrainedBox(
constraints: BoxConstraints(
minWidth: 48,
minHeight: 48,
),
child: Center(
child: Icon(Icons.favorite_border),
),
),
),
),
),
),
);
}
}ConstrainedBox sets minimum dimensions without forcing the control to be exactly that size. The Center keeps the icon visually aligned. InkWell supplies Material feedback, such as a splash effect. The Semantics widget gives the custom control a name and button role.
Avoid overlapping targets
Increasing a target can create problems when controls are close together. Two adjacent buttons should have separate, predictable hit regions. Add spacing between them instead of making their regions overlap.
A control can also become too small because its parent imposes tight constraints. Check the constraints passed by Row, Column, GridView, and custom layout code. Test the final rendered layout rather than checking only the source dimensions.
Check target sizes automatically
Flutter's widget tests include guidelines for common target sizes. Enable semantics before running these checks:
testWidgets('buttons meet minimum tap sizes', (tester) async {
final semanticsHandle = tester.ensureSemantics();
await tester.pumpWidget(
const MaterialApp(home: TapTargetDemo()),
);
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
semanticsHandle.dispose();
});These tests catch controls that are too small or lack accessible labels. They do not replace testing on a real device. Try the interface with different screen sizes, text scales, and input methods.
Exercise
An icon is 24 by 24 pixels inside a custom GestureDetector. What should you change first?
Show answer
Give the interactive region a minimum size of at least 48 by 48 logical pixels, keep the icon centered, and add a semantic label and button role.