handler<T extends Intent> static method

VoidCallback? handler<T extends Intent>(
  1. BuildContext context,
  2. T intent
)

Returns a VoidCallback handler that invokes the bound action for the given intent if the action is enabled, and returns null if the action is not enabled, or no matching action is found.

This is intended to be used in widgets which have something similar to an onTap handler, which takes a VoidCallback, and can be set to the result of calling this function.

Creates a dependency on the Actions widget that maps the bound action so that if the actions change, the context will be rebuilt and find the updated action.

The value returned from the Action.invoke method is discarded when the returned callback is called. If the return value is needed, consider using Actions.invoke instead.

Implementation

static VoidCallback? handler<T extends Intent>(BuildContext context, T intent) {
  final Action<T>? action = Actions.maybeFind<T>(context);
  if (action != null && action._isEnabled(intent, context)) {
    return () {
      // Could be that the action was enabled when the closure was created,
      // but is now no longer enabled, so check again.
      if (action._isEnabled(intent, context)) {
        Actions.of(context).invokeAction(action, intent, context);
      }
    };
  }
  return null;
}