text stringlengths 1 474 |
|---|
} |
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> |
The Shortcuts widget uses the Focus widget’s context and Actions.invoke to |
find which action to invoke. If the Shortcuts widget doesn’t find a matching |
intent type in the first Actions widget encountered, it considers the next |
ancestor Actions widget, and so on, until it reaches the root of the widget |
tree, or finds a matching intent type and invokes the corresponding action.<topic_end> |
<topic_start> |
Invoking Actions |
The actions system has several ways to invoke actions. By far the most common |
way is through the use of a Shortcuts widget covered in the previous section, |
but there are other ways to interrogate the actions subsystem and invoke an |
action. It’s possible to invoke actions that are not bound to keys.For instance, to find an action associated with an intent, you can use: |
<code_start>Action<SelectAllIntent>? selectAll = |
Actions.maybeFind<SelectAllIntent>(context);<code_end> |
This returns an Action associated with the SelectAllIntent type if one is |
available in the given context. If one isn’t available, it returns null. If |
an associated Action should always be available, then use find instead of |
maybeFind, which throws an exception when it doesn’t find a matching Intent |
type.To invoke the action (if it exists), call: |
<code_start>Object? result; |
if (selectAll != null) { |
result = |
Actions.of(context).invokeAction(selectAll, const SelectAllIntent()); |
}<code_end> |
Combine that into one call with the following: |
<code_start>Object? result = |
Actions.maybeInvoke<SelectAllIntent>(context, const SelectAllIntent());<code_end> |
Sometimes you want to invoke an action as a |
result of pressing a button or another control. |
You can do this with the Actions.handler function. |
If the intent has a mapping to an enabled action, |
the Actions.handler function creates a handler closure. |
However, if it doesn’t have a mapping, it returns null. |
This allows the button to be disabled if |
there is no enabled action that matches in the context. |
<code_start>@override |
Widget build(BuildContext context) { |
return Actions( |
actions: <Type, Action<Intent>>{ |
SelectAllIntent: SelectAllAction(model), |
}, |
child: Builder( |
builder: (context) => TextButton( |
onPressed: Actions.handler<SelectAllIntent>( |
context, |
SelectAllIntent(controller: controller), |
), |
child: const Text('SELECT ALL'), |
), |
), |
); |
}<code_end> |
The Actions widget only invokes actions when isEnabled(Intent intent) |
returns true, allowing the action to decide if the dispatcher should consider it |
for invocation. If the action isn’t enabled, then the Actions widget gives |
another enabled action higher in the widget hierarchy (if it exists) a chance to |
execute.The previous example uses a Builder because Actions.handler and |
Actions.invoke (for example) only finds actions in the provided context, and |
if the example passes the context given to the build function, the framework |
starts looking above the current widget. Using a Builder allows the |
framework to find the actions defined in the same build function.You can invoke an action without needing a BuildContext, but since the |
Actions widget requires a context to find an enabled action to invoke, you |
need to provide one, either by creating your own Action instance, or by |
finding one in an appropriate context with Actions.find.To invoke the action, pass the action to the invoke method on an |
ActionDispatcher, either one you created yourself, or one retrieved from an |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.