handler<T extends Intent> method Null safety

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.

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)) {
    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)) {
        Actions.of(context).invokeAction(action, intent, context);
      }
    };
  }
  return null;
}