text
stringlengths
1
474
almost every scrollable custom widget is built
using one of these, it works with them as well.If you need to implement custom scroll behavior,
you can use the Listener widget, which lets you
customize how your UI reacts to the scroll wheel.
<code_start>return Listener(
onPointerSignal: (event) {
if (event is PointerScrollEvent) print(event.scrollDelta.dy);
},
child: ListView(),
);<code_end>
<topic_end>
<topic_start>
Tab traversal and focus interactions
Users with physical keyboards expect that they can use
the tab key to quickly navigate your application,
and users with motor or vision differences often rely
completely on keyboard navigation.There are two considerations for tab interactions:
how focus moves from widget to widget, known as traversal,
and the visual highlight shown when a widget is focused.Most built-in components, like buttons and text fields,
support traversal and highlights by default.
If you have your own widget that you want included in
traversal, you can use the FocusableActionDetector widget
to create your own controls. It combines the functionality
of Actions, Shortcuts, MouseRegion, and
Focus widgets to create a detector that defines actions
and key bindings, and provides callbacks for handling focus
and hover highlights.
<code_start>class _BasicActionDetectorState extends State<BasicActionDetector> {
bool _hasFocus = false;
@override
Widget build(BuildContext context) {
return FocusableActionDetector(
onFocusChange: (value) => setState(() => _hasFocus = value),
actions: <Type, Action<Intent>>{
ActivateIntent: CallbackAction<Intent>(onInvoke: (intent) {
print('Enter or Space was pressed!');
return null;
}),
},
child: Stack(
clipBehavior: Clip.none,
children: [
const FlutterLogo(size: 100),
// Position focus in the negative margin for a cool effect
if (_hasFocus)
Positioned(
left: -4,
top: -4,
bottom: -4,
right: -4,
child: _roundedBorder(),
)
],
),
);
}
}<code_end>
<topic_end>
<topic_start>Controlling traversal order
To get more control over the order that
widgets are focused on when the user presses tab,
you can use FocusTraversalGroup to define sections
of the tree that should be treated as a group when tabbing.For example, you might to tab through all the fields in
a form before tabbing to the submit button:
<code_start>return Column(children: [
FocusTraversalGroup(
child: MyFormWithMultipleColumnsAndRows(),
),
SubmitButton(),
]);<code_end>
Flutter has several built-in ways to traverse widgets and groups,
defaulting to the ReadingOrderTraversalPolicy class.
This class usually works well, but it’s possible to modify this
using another predefined TraversalPolicy class or by creating
a custom policy.<topic_end>
<topic_start>
Keyboard accelerators
In addition to tab traversal, desktop and web users are accustomed
to having various keyboard shortcuts bound to specific actions.
Whether it’s the Delete key for quick deletions or
Control+N for a new document, be sure to consider the different
accelerators your users expect. The keyboard is a powerful
input tool, so try to squeeze as much efficiency from it as you can.
Your users will appreciate it!Keyboard accelerators can be accomplished in a few ways in Flutter
depending on your goals.If you have a single widget like a TextField or a Button that
already has a focus node, you can wrap it in a KeyboardListener
or a Focus widget and listen for keyboard events:
<code_start> @override
Widget build(BuildContext context) {
return Focus(
onKeyEvent: (node, event) {
if (event is KeyDownEvent) {
print(event.logicalKey);
}
return KeyEventResult.ignored;
},
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: const TextField(
decoration: InputDecoration(