getViewPosition static method

void getViewPosition(
  1. String listenerId,
  2. String clientElementId,
  3. double pixelRatio,
  4. int screenWidth,
  5. int screenHeight,
)

Implementation

static void getViewPosition(String listenerId, String clientElementId,
    double pixelRatio, int screenWidth, int screenHeight) {
  try {
    plotlineDebugLog("PlotlineDebug: getViewPosition: called: $listenerId, $clientElementId, $pixelRatio, $screenWidth, $screenHeight");
    screenHeight = (MediaQuery.of(_ctx).size.height * pixelRatio).round();
    screenWidth = (MediaQuery.of(_ctx).size.width * pixelRatio).round();
    if (!clientElementId.contains("<'") && !clientElementId.contains("'>")) {
      clientElementId = "[<'$clientElementId'>]";
    }
    BuildContext? element = findViewByKey(clientElementId, _ctx);
    if (element != null) {
      plotlineDebugLog("PlotlineDebug: getViewPosition: element != null: $listenerId, $clientElementId, $pixelRatio, $screenWidth, $screenHeight");
      RenderBox box = element.findRenderObject() as RenderBox;
      Offset position = box.localToGlobal(Offset.zero);
      final bool inBounds = isWithinBounds(
          element, position, pixelRatio, screenWidth, screenHeight);
      final bool isVisible = visibilityMap[clientElementId] == 100.0;
      if (inBounds && isVisible) {
        plotlineDebugLog("PlotlineDebug: getViewPosition: isWithinBounds: $listenerId, $clientElementId, $pixelRatio, $screenWidth, $screenHeight");
        Map<String, int?> positionMap = {
          "x": (position.dx * pixelRatio).round(),
          "y": (position.dy * pixelRatio).round(),
          "width": ((element.size?.width ?? 0) * pixelRatio).round(),
          "height": ((element.size?.height ?? 0) * pixelRatio).round()
        };
        plotlineDebugLog("PlotlineDebug: getViewPosition: positionMap: $positionMap $listenerId, $clientElementId, $pixelRatio, $screenWidth, $screenHeight");
        plotlineChannel.invokeMethod('getViewPosition', <String, dynamic>{
          "listenerId": listenerId,
          "position": positionMap
        });
        return;
      }
      if (shouldEnableDebug) {
        // Only one of the two rejections means the element is really off
        // screen; downstream they are indistinguishable.
        plotlineDebugLog(
            "PlotlineDebug: getViewPosition: REJECTED for $clientElementId "
            "inBounds=$inBounds visibility=${visibilityMap[clientElementId]} "
            "pos=(${position.dx},${position.dy}) "
            "size=${box.hasSize ? box.size : 'unlaid-out'}: $listenerId");
      }
    } else {
      plotlineDebugLog("PlotlineDebug: getViewPosition: element not found: $listenerId, $clientElementId, $pixelRatio, $screenWidth, $screenHeight");
    }

    // Passing values which make the object get ignored
    Map<String, int?> positionMapDefault = {
      "x": -1,
      "y": -1,
      "width": -1,
      "height": -1
    };
    if (shouldEnableDebug) {
      // Native reads this as a degenerate rect, not a null, so the flow ends
      // here rather than the step being skipped.
      plotlineDebugLog(
          "PlotlineDebug: getViewPosition: SENTINEL -1 returned for "
          "$clientElementId: $listenerId");
    }
    plotlineChannel.invokeMethod('getViewPosition', <String, dynamic>{
      "listenerId": listenerId,
      "position": positionMapDefault
    });
  } catch (e) {
    plotlineDebugLog("Error in getViewPosition: $e");
  }
}