invokeActionOnFocusedWidget function

(bool, Object?) invokeActionOnFocusedWidget(
  1. Intent intent
)

Invokes an action on the currently focused widget.

Attempts to find and invoke an action associated with the given intent on the widget that currently has focus. This is useful for triggering actions programmatically.

Parameters:

  • intent (Intent, required): The intent to invoke.

Returns: (bool enabled, Object? invokeResult) — a record containing whether the action was enabled and the result of invoking it.

Example:

final (enabled, result) = invokeActionOnFocusedWidget(
  ActivateIntent(),
);

Implementation

(bool enabled, Object? invokeResult) invokeActionOnFocusedWidget(
    Intent intent) {
  final context = primaryFocus?.context;
  if (context != null) {
    Action<Intent>? action = Actions.maybeFind<Intent>(context, intent: intent);
    if (action != null) {
      final (bool enabled, Object? invokeResult) =
          Actions.of(context).invokeActionIfEnabled(action, intent);
      return (enabled, invokeResult);
    }
  }
  return (false, null);
}