text stringlengths 1 372 |
|---|
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 |
existing actions widget using the actions.of(context) method. check whether |
the action is enabled before calling invoke. of course, you can also just call |
invoke on the action itself, passing an intent, but then you opt out of any |
services that an action dispatcher might provide (like logging, undo/redo, and |
so on). |
<topic_end> |
<topic_start> |
action dispatchers |
most of the time, you just want to invoke an action, have it do its thing, and |
forget about it. sometimes, however, you might want to log the executed actions. |
this is where replacing the default ActionDispatcher with a custom dispatcher |
comes in. you pass your ActionDispatcher to the actions widget, and it |
invokes actions from any actions widgets below that one that doesn’t set a |
dispatcher of its own. |
the first thing actions does when invoking an action is look up the |
ActionDispatcher and pass the action to it for invocation. if there is none, |
it creates a default ActionDispatcher that simply invokes the action. |
if you want a log of all the actions invoked, however, you can create your own |
LoggingActionDispatcher to do the job: |
<code_start> |
class LoggingActionDispatcher extends ActionDispatcher { |
@override |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.