Sub topic 13
Composing a Custom Tappable Widget
Assume you know basic Dart, widget trees, StatefulWidget, setState, and layout widgets such as Padding and Column. This chapter combines those pieces into a reusable tappable component.
A custom tappable widget should handle more than the callback. It should also provide:
- Visible pressed feedback.
- A large enough touch target.
- A clear semantic role for screen readers.
- A disabled state when no action is available.
- A consistent shape and appearance.
Choose the right building blocks
InkWell is usually the best foundation for a custom Material-style tap target. It recognizes taps and paints ripple feedback on a Material ancestor. Semantics describes the widget to accessibility services. ConstrainedBox helps guarantee a usable minimum size.
| Requirement | Widget or property |
|---|---|
| Tap callback | `InkWell.onTap` |
| Ripple feedback | `Material` and `InkWell` |
| Accessibility role | `Semantics(button: true)` |
| Disabled state | A null `onTap` callback |
| Minimum target size | `BoxConstraints(minHeight: 48, minWidth: 48)` |
Avoid adding a GestureDetector around InkWell for the same tap. Two recognizers can compete for the pointer, and the extra layer does not improve the interaction.
Build a reusable tappable card
The following widget accepts any child, so callers can use text, icons, or a more complex layout. The callback is nullable. When it is null, the widget becomes disabled.
import 'package:flutter/material.dart';
void main() {
runApp(const DemoApp());
}
class TappableCard extends StatelessWidget {
const TappableCard({
required this.label,
required this.child,
this.onTap,
super.key,
});
final String label;
final Widget child;
final VoidCallback? onTap;
@override
Widget build(BuildContext context) {
final enabled = onTap != null;
final colors = Theme.of(context).colorScheme;
return Semantics(
button: true,
enabled: enabled,
label: label,
child: Tooltip(
message: label,
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 48,
minHeight: 48,
),
child: Material(
color: enabled
? colors.surfaceContainerHighest
: colors.surfaceContainerHighest.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(12),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.all(16),
child: child,
),
),
),
),
),
);
}
}
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Custom tappable widget')),
body: Center(
child: TappableCard(
label: 'Open details',
onTap: () => debugPrint('Details opened'),
child: const Text('Open details'),
),
),
),
);
}
}Semantics.label gives the control a useful spoken name. The button flag communicates its role. The enabled value must match the callback so assistive technology does not announce an inactive control as usable.
The 48-pixel constraint is a practical minimum for touch interaction. The visible content can be smaller, but the interactive region should remain easy to hit.
Common mistakes
- Placing
InkWellabove noMaterialwidget, which prevents the ripple from appearing. - Setting
onTapto an empty callback when the control should be disabled. Usenull. - Giving the control only a small visible child and forgetting the minimum hit area.
- Omitting a meaningful label when the child contains only an icon.
- Using a
GestureDetectorwhen Material feedback and button semantics are required.
Exercise
Why does TappableCard use both Semantics and InkWell?
Show answer
InkWell recognizes the tap and paints Material feedback, while Semantics exposes the control's role, label, and enabled state to accessibility services.