findWidgetDetails static method

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

Implementation

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

  if (key != null && key.currentContext != null) {
    RenderBox box = key.currentContext!.findRenderObject() as RenderBox;
    Offset position = box.localToGlobal(Offset.zero);

    // if position is out of bounds, return false
    if (position.dx < 0 ||
        position.dy < 0 ||
        box.size.height == 0 ||
        box.size.width == 0) {
      NLogger.w(
        'Widget position for label $label is out of bounds: $position, size: ${box.size}',
      );
      return {
        'result': false,
        'message': 'Widget position is partially or completely out of bounds',
      };
    }

    NLogger.i('Finding widget position for label to show overlay: $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');
    // }

    box = key.currentContext!.findRenderObject() as RenderBox;
    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,
    };
  }
}