Sub topic 12
Adapting to Touch, Mouse and Keyboard
Start with shared actions
This chapter assumes basic Dart, widget trees, and familiarity with Scaffold, Row, Column, and ListView.
A responsive layout changes its structure as space changes. An adaptive interface also changes how people interact with it. A phone user mainly taps and swipes. A desktop user may hover, click, tab between controls, and press keyboard shortcuts.
Keep the action itself independent from the input method. For example, “save the document” should be one callback. A tap, mouse click, or keyboard command should call that same callback.
| Input method | Useful response | Important rule |
|---|---|---|
| Touch | Tappable controls and swipe gestures | Make targets large enough to tap |
| Mouse | Hover feedback, cursor changes, right-click support | Do not require hover to understand the UI |
| Keyboard | Focus, activation keys, and shortcuts | Every important action must be reachable without a pointer |
Support touch without making controls awkward
Material components such as FilledButton, IconButton, and ListTile already provide many touch behaviors. Prefer them over a custom GestureDetector when they match the meaning of the control.
A touch target should generally be at least 48 logical pixels in each direction. The visible icon can be smaller, but its surrounding interactive area should remain large.
Use Semantics when a custom control does not communicate its role clearly. A screen reader should know that the control is a button and what it does.
Add mouse and keyboard behavior
FocusableActionDetector combines focus and hover detection. Shortcuts maps keyboard input to an intent, and Actions maps that intent to a callback.
The following widget activates from a tap, Enter, or Space. It also shows hover and focus feedback:
import 'package:flutter/material.dart';
class AdaptiveActionButton extends StatefulWidget {
const AdaptiveActionButton({
super.key,
required this.label,
required this.onPressed,
});
final String label;
final VoidCallback onPressed;
@override
State<AdaptiveActionButton> createState() => _AdaptiveActionButtonState();
}
class _AdaptiveActionButtonState extends State<AdaptiveActionButton> {
bool _hovered = false;
bool _focused = false;
@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: const <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.enter): ActivateIntent(),
SingleActivator(LogicalKeyboardKey.space): ActivateIntent(),
},
child: Actions(
actions: <Type, Action<Intent>>{
ActivateIntent: CallbackAction<ActivateIntent>(
onInvoke: (_) {
widget.onPressed();
return null;
},
),
},
child: FocusableActionDetector(
mouseCursor: SystemMouseCursors.click,
onShowHoverHighlight: (value) {
setState(() => _hovered = value);
},
onShowFocusHighlight: (value) {
setState(() => _focused = value);
},
child: Semantics(
button: true,
label: widget.label,
child: GestureDetector(
onTap: widget.onPressed,
child: DecoratedBox(
decoration: BoxDecoration(
color: _hovered
? Colors.blue.shade700
: Colors.blue.shade600,
border: _focused
? Border.all(color: Colors.orange, width: 3)
: null,
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 14,
),
child: Text(
widget.label,
style: const TextStyle(color: Colors.white),
),
),
),
),
),
),
),
);
}
}The focus indicator must remain visible when the user navigates with Tab. Do not use hover color as the only indication of selection because touch users never receive hover events.
Choose shortcuts carefully
Keyboard shortcuts should match common platform expectations. For example, Control-S is common on Windows and Linux, while Command-S is common on macOS. Flutter's SingleActivator can describe modifier keys, but avoid overriding shortcuts that users expect to work globally.
Use MouseRegion when you only need pointer movement or hover information. Use FocusableActionDetector when the same control also needs focus and keyboard behavior.
Exercise
Why should a custom button use one shared callback for tap and keyboard activation?
Show answer
A shared callback keeps the action consistent across input methods and prevents tap and keyboard paths from developing different behavior.