nativeFindWidgetPositionByLabel static method

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

Implementation

static Future<Map<String, dynamic>> nativeFindWidgetPositionByLabel({
  required String label,
  required num scaleX,
  required num scaleY,
  num? statusBarHeight,
  required Size screenSize,
}) async {
  if (await _checkPageScrollStatus(label)) {
    NLogger.w('Widget $label is currently scrolling, cannot track position');
    return {'result': false, 'message': 'Widget is currently scrolling'};
  }

  final stable = await _isWidgetStable(
    label,
    stabilityDuration: const Duration(seconds: 1),
    checkInterval: const Duration(milliseconds: 100),
  );
  if (!stable) {
    return {'result': false, 'message': 'Widget position changed during tracking'};
  }

  final box = _resolveRenderBox(label);
  if (box == null || !box.hasSize) return {'result': false};

  final pos = _globalTopLeft(box);
  final size = box.size;

  // ✅ New check for NaN position values
  if (pos.dx.isNaN ||
      pos.dy.isNaN ||
      pos.dx.isInfinite ||
      pos.dy.isInfinite ||
      size.width.isNaN ||
      size.height.isNaN ||
      size.width.isInfinite ||
      size.height.isInfinite) {
    NLogger.w('Widget $label has NaN position values: $pos');
    return {'result': false, 'message': 'Widget position is NaN'};
  }

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

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