text
stringlengths
1
372
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(
border: OutlineInputBorder(),
),
),
),
);
}
}
<code_end>
if you’d like to apply a set of keyboard shortcuts to a
large section of the tree, you can use the shortcuts widget:
<code_start>
// define a class for each type of shortcut action you want
class CreateNewItemIntent extends intent {
const CreateNewItemIntent();
}
widget build(BuildContext context) {
return shortcuts(
// bind intents to key combinations
shortcuts: const <shortcutactivator, intent>{
SingleActivator(LogicalKeyboardKey.keyN, control: true):
CreateNewItemIntent(),
},