text stringlengths 1 372 |
|---|
directly perform the action (such as set a value or save a file). in a larger |
application, however, things are more complex: the code for invoking the action, |
and the code for the action itself might need to be in different places. |
shortcuts (key bindings) might need definition at a level that knows nothing |
about the actions they invoke. |
that’s where flutter’s actions and shortcuts system comes in. it allows |
developers to define actions that fulfill intents bound to them. in this |
context, an intent is a generic action that the user wishes to perform, and an |
intent class instance represents these user intents in flutter. an |
intent can be general purpose, fulfilled by different actions in different |
contexts. an action can be a simple callback (as in the case of |
the CallbackAction) or something more complex that integrates with entire |
undo/redo architectures (for example) or other logic. |
shortcuts are key bindings that activate by pressing a key or combination |
of keys. the key combinations reside in a table with their bound intent. when |
the shortcuts widget invokes them, it sends their matching intent to the |
actions subsystem for fulfillment. |
to illustrate the concepts in actions and shortcuts, this article creates a |
simple app that allows a user to select and copy text in a text field using both |
buttons and shortcuts. |
<topic_end> |
<topic_start> |
why separate actions from intents? |
you might wonder: why not just map a key combination directly to an action? why |
have intents at all? this is because it is useful to have a separation of |
concerns between where the key mapping definitions are (often at a high level), |
and where the action definitions are (often at a low level), and because it is |
important to be able to have a single key combination map to an intended |
operation in an app, and have it adapt automatically to whichever action |
fulfills that intended operation for the focused context. |
for instance, flutter has an ActivateIntent widget that maps each type of |
control to its corresponding version of an ActivateAction (and that executes |
the code that activates the control). this code often needs fairly private |
access to do its work. if the extra layer of indirection that intents provide |
didn’t exist, it would be necessary to elevate the definition of the actions to |
where the defining instance of the shortcuts widget could see them, causing |
the shortcuts to have more knowledge than necessary about which action to |
invoke, and to have access to or provide state that it wouldn’t necessarily have |
or need otherwise. this allows your code to separate the two concerns to be more |
independent. |
intents configure an action so that the same action can serve multiple uses. an |
example of this is DirectionalFocusIntent, which takes a direction to move |
the focus in, allowing the DirectionalFocusAction to know which direction to |
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>{ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.