nativeFindWidgetPositionByLabel static method

Future<Map<String, dynamic>> nativeFindWidgetPositionByLabel({
  1. required String label,
  2. required num scaleX,
  3. required num scaleY,
  4. num? statusBarHeight,
})

Implementation

static Future<Map<String, dynamic>> nativeFindWidgetPositionByLabel({
  required String label,
  required num scaleX,
  required num scaleY,
  num? statusBarHeight,
}) async {
  final GlobalKey? key = _widgetKeys[label];

  // OverlayEntry blocker = OverlayEntry(
  //   builder: (_) => const ModalBarrier(
  //     dismissible: false,
  //     color: Colors.transparent,
  //   ),
  // );

  // if (Nudge.nudgeNavigatorKey.currentContext != null) {
  //   Nudge.nudgeNavigatorKey.currentState?.overlay?.insert(blocker);
  // }

  if (key != null && key.currentContext != null) {
    NLogger.i('Finding widget position for label: $label');
    ScrollableState? scrollableState = Scrollable.maybeOf(
      key.currentContext!,
      axis: Axis.vertical,
    );

    if (scrollableState != null) {
      NLogger.i('Scrollable found for label, scrolling into view');
      await Scrollable.ensureVisible(
        key.currentContext!,
        duration: const Duration(
          milliseconds: 300,
        ),
        alignment: 0.5,
        curve: Curves.easeInOut,
      );
      NLogger.i('Scrolled into view for label: $label');
    }

    // if (Nudge.nudgeNavigatorKey.currentContext != null) {
    //   blocker.remove();
    // }

    final RenderBox box = key.currentContext!.findRenderObject() as RenderBox;

    final Offset position = box.localToGlobal(Offset.zero);

    NLogger.i(
      'Widget position for label $label: x=${position.dx}, y=${position.dy}, width=${box.size.width}, height=${box.size.height}',
    );

    return {
      'result': true,
      'x': position.dx * scaleX,
      'y': position.dy * scaleY + (statusBarHeight ?? 0),
      'width': box.size.width * scaleX,
      'height': box.size.height * scaleY,
    };
  } else {
    return {
      'result': false,
    };
  }
}