text
stringlengths
1
372
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');
}
return result;
}
}
<code_end>
now, every time the shortcuts widget handles a shortcut, it prints out the key
event and relevant context.
<topic_end>
<topic_start>
actions
actions allow for the definition of operations that the application can
perform by invoking them with an intent. actions can be enabled or disabled,
and receive the intent instance that invoked them as an argument to allow
configuration by the intent.
<topic_end>
<topic_start>
defining actions
actions, in their simplest form, are just subclasses of Action<Intent> with an
invoke() method. here’s a simple action that simply invokes a function on the
provided model:
<code_start>
class SelectAllAction extends Action<SelectAllIntent> {
SelectAllAction(this.model);
final model model;
@override
void invoke(covariant SelectAllIntent intent) => model.selectAll();
}
<code_end>
or, if it’s too much of a bother to create a new class, use a CallbackAction:
<code_start>
CallbackAction(onInvoke: (intent) => model.selectAll());
<code_end>
once you have an action, you add it to your application using the actions
widget, which takes a map of intent types to actions:
<code_start>
@override
widget build(BuildContext context) {
return actions(
actions: <type, Action<Intent>>{
SelectAllIntent: SelectAllAction(model),
},
child: child,
);
}
<code_end>