text
stringlengths
1
474
move the focus. Just be careful: don’t pass state in the Intent that applies
to all invocations of an Action: that kind of state should be passed to the
constructor of the Action itself, to keep the Intent from needing to know
too much.<topic_end>
<topic_start>
Why not use callbacks?
You also might wonder: why not just use a callback instead of an Action
object? The main reason is that it’s useful for actions to decide whether they
are enabled by implementing isEnabled. Also, it is often helpful if the key
bindings, and the implementation of those bindings, are in different places.If all you need are callbacks without the flexibility of Actions and
Shortcuts, you can use the CallbackShortcuts widget:
<code_start>@override
Widget build(BuildContext context) {
return CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
const SingleActivator(LogicalKeyboardKey.arrowUp): () {
setState(() => count = count + 1);
},
const SingleActivator(LogicalKeyboardKey.arrowDown): () {
setState(() => count = count - 1);
},
},
child: Focus(
autofocus: true,
child: Column(
children: <Widget>[
const Text('Press the up arrow key to add to the counter'),
const Text('Press the down arrow key to subtract from the counter'),
Text('count: $count'),
],
),
),
);
}<code_end>
<topic_end>
<topic_start>
Shortcuts
As you’ll see below, actions are useful on their own, but the most common use
case involves binding them to a keyboard shortcut. This is what the Shortcuts
widget is for.It is inserted into the widget hierarchy to define key combinations that
represent the user’s intent when that key combination is pressed. To convert
that intended purpose for the key combination into a concrete action, the
Actions widget used to map the Intent to an Action. For instance, you can
define a SelectAllIntent, and bind it to your own SelectAllAction or to your
CanvasSelectAllAction, and from that one key binding, the system invokes
either one, depending on which part of your application has focus. Let’s see how
the key binding part works:
<code_start>@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyA):
const SelectAllIntent(),
},
child: Actions(
dispatcher: LoggingActionDispatcher(),
actions: <Type, Action<Intent>>{
SelectAllIntent: SelectAllAction(model),
},
child: Builder(
builder: (context) => TextButton(
onPressed: Actions.handler<SelectAllIntent>(
context,
const SelectAllIntent(),
),
child: const Text('SELECT ALL'),
),
),
),
);
}<code_end>
The map given to a Shortcuts widget maps a LogicalKeySet (or a
ShortcutActivator, see note below) to an Intent instance. The logical key
set defines a set of one or more keys, and the intent indicates the intended
purpose of the keypress. The Shortcuts widget looks up key presses in the map,
to find an Intent instance, which it gives to the action’s invoke() method.info Note
ShortcutActivator is a replacement for LogicalKeySet.
It allows for more flexible and correct activation of shortcuts.
LogicalKeySet is a ShortcutActivator, of course, but
there is also SingleActivator, which takes a single key and the
optional modifiers to be pressed before the key.
Then there is CharacterActivator, which activates a shortcut based on the
character produced by a key sequence, instead of the logical keys themselves.
ShortcutActivator is also meant to be subclassed to allow for
custom ways of activating shortcuts from key events.<topic_end>
<topic_start>
The ShortcutManager
The shortcut manager, a longer-lived object than the Shortcuts widget, passes
on key events when it receives them. It contains the logic for deciding how to
handle the keys, the logic for walking up the tree to find other shortcut
mappings, and maintains a map of key combinations to intents.While the default behavior of the ShortcutManager is usually desirable, the
Shortcuts widget takes a ShortcutManager that you can subclass to customize
its functionality.For example, if you wanted to log each key that a Shortcuts widget handled,
you could make a LoggingShortcutManager:
<code_start>class LoggingShortcutManager extends ShortcutManager {
@override
KeyEventResult handleKeypress(BuildContext context, KeyEvent event) {
final KeyEventResult result = super.handleKeypress(context, event);
if (result == KeyEventResult.handled) {
print('Handled shortcut $event in $context');