dispatchPointerEvent method

*[<Null safety>](https://dart.dev/null-safety)*

Future<void> dispatchPointerEvent ({required int viewId, required dynamic pointer, int? viewRef})

Dispatch PointerEvent event to viewId.

Implementation

Future<void> dispatchPointerEvent(
    {required int viewId,
    required PointerEvent pointer,
    int? viewRef}) async {
  if (!_hasFuchsiaEventPhase(pointer)) {
    return;
  }

  // We use the global position in the parent's local coordinate system.
  // The injection viewport is set up to coincide with the local coordinate
  // system, and all child-specific transforms are handled on the server.
  final x = pointer.position.dx;
  final y = pointer.position.dy;

  // Use the logical space of the parent view as the injection viewport.
  // It means that pointer coordinates, in the parent's view, can be used
  // verbatim for injecting into a child view. The fuchsia.ui.pointerinjector
  // server handles the pointer coordinate transforms for the child view.
  //
  // Note that the Flutter instance's logical space can change size, but since
  // the logical space *always* has its origin at Offset.zero, a size change
  // does not need a new viewport, since the viewport merely anchors pointer
  // coordinates received by the Flutter instance.
  ui.Size window = ui.window.physicalSize / ui.window.devicePixelRatio;

  final phase = pointer is PointerDownEvent
      ? EventPhase.add
      : pointer is PointerUpEvent
          ? EventPhase.remove
          : pointer is PointerMoveEvent
              ? EventPhase.change
              : EventPhase.cancel;

  final args = _makeInjectArgs(
      viewId: viewId,
      x: x,
      y: y,
      phase: _getEventPhaseValue(phase),
      pointerId: ((pointer.device << 32) >> 32),
      // `traceFlowId` and `pointer.pointer` are program specific nonces.
      traceFlowId: pointer.pointer,
      logicalWidth: window.width,
      logicalHeight: window.height,
      timestamp: pointer.timeStamp.inMicroseconds * 1000,
      viewRef: viewRef);

  return platformViewChannel.invokeMethod(
      'View.pointerinjector.inject', args);
}