navigateOnClick static method

StreamSubscription? navigateOnClick(
  1. UIElement element,
  2. String? route, [
  3. Map<String, String>? parameters,
  4. ParametersProvider? parametersProvider,
  5. bool force = false,
])

Register a onClick listener in element to navigate to route with parameters.

Implementation

static StreamSubscription? navigateOnClick(UIElement element, String? route,
    [Map<String, String>? parameters,
    ParametersProvider? parametersProvider,
    bool force = false]) {
  var paramsStr = encodeQueryString(parameters);

  var attrRoute = element.getAttribute('__navigate__route');
  var attrParams = element.getAttribute('__navigate__parameters');

  if (route != attrRoute || paramsStr != attrParams) {
    element.setAttribute('__navigate__route', route!);
    element.setAttribute('__navigate__parameters', paramsStr);

    var subscriptionHolder = <StreamSubscription>[];

    var subscription = element.onClick.listen((e) {
      var elemRoute = element.getAttribute('__navigate__route');
      var elemRouteParams = element.getAttribute('__navigate__parameters');

      if (elemRoute == route && elemRouteParams == paramsStr) {
        navigateTo(route,
            parameters: parameters,
            parametersProvider: parametersProvider,
            force: force);
      } else if (subscriptionHolder.isNotEmpty) {
        var subscription = subscriptionHolder[0];
        subscription.cancel();
      }
    });

    subscriptionHolder.add(subscription);

    if (element.style.cursor.isEmpty) {
      element.style.cursor = 'pointer';
    }

    return subscription;
  }

  return null;
}