Action<T extends Intent>.overridable constructor

Action<T extends Intent>.overridable({
  1. required Action<T> defaultAction,
  2. required BuildContext context,
})

Creates an Action that allows itself to be overridden by the closest ancestor Action in the given context that handles the same Intent, if one exists.

When invoked, the resulting Action tries to find the closest Action in the given context that handles the same type of Intent as the defaultAction, then calls its Action.invoke method. When no override Actions can be found, it invokes the defaultAction.

An overridable action delegates everything to its override if one exists, and has the same behavior as its defaultAction otherwise. For this reason, the override has full control over whether and how an Intent should be handled, or a key event should be consumed. An override Action's callingAction property will be set to the Action it currently overrides, giving it access to the default behavior. See the callingAction property for an example.

The context argument is the BuildContext to find the override with. It is typically a BuildContext above the Actions widget that contains this overridable Action.

The defaultAction argument is the Action to be invoked where there's no ancestor Actions can't be found in context that handle the same type of Intent.

This is useful for providing a set of default Actions in a leaf widget to allow further overriding, or to allow the Intent to propagate to parent widgets that also support this Intent.

This sample shows how to implement a rudimentary CopyableText widget that responds to Ctrl-C by copying its own content to the clipboard.

if CopyableText is to be provided in a package, developers using the widget may want to change how copying is handled. As the author of the package, you can enable that by making the corresponding Action overridable. In the second part of the code sample, three CopyableText widgets are used to build a verification code widget which overrides the "copy" action by copying the combined numbers from all three CopyableText widgets.

link

To create a local project with this code sample, run:
flutter create --sample=widgets.Action.overridable.1 mysample

Implementation

factory Action.overridable({
  required Action<T> defaultAction,
  required BuildContext context,
}) {
  return defaultAction._makeOverridableAction(context);
}