Action<T extends Intent>.overridable constructor Null safety

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 implements a custom text input field that handles the DeleteCharacterIntent intent, as well as a US telephone number input widget that consists of multiple text fields for area code, prefix and line number. When the backspace key is pressed, the phone number input widget sends the focus to the preceding text field when the currently focused field becomes empty.
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);
}