getWidgetRect method

ScreenRect? getWidgetRect(
  1. GlobalKey<State<StatefulWidget>> key, {
  2. ScreenRect? windowBounds,
})

Get a widget's bounds in screen coordinates.

key - GlobalKey attached to the widget. windowBounds - Optional override for the palette's window bounds.

Returns null if the widget is not mounted or window bounds are not set.

Implementation

ScreenRect? getWidgetRect(GlobalKey key, {ScreenRect? windowBounds}) {
  final bounds = windowBounds ?? this.windowBounds;
  if (bounds == null) return null;

  final localRect = getLocalRect(key);
  if (localRect == null) return null;

  // Convert the local rect's origin to screen coordinates
  final screenOrigin = bounds.localToScreen(localRect.topLeft);

  // Create a new rect at screen position
  // Note: The rect dimensions stay the same, just the position changes
  if (isMacOS) {
    // On macOS, we need to account for the coordinate flip
    // Screen origin is at the top-left of the widget visually
    return ScreenRect(
      Rect.fromLTWH(
        screenOrigin.dx,
        screenOrigin.dy - localRect.height, // Adjust for macOS Y flip
        localRect.width,
        localRect.height,
      ),
      isMacOS: true,
    );
  } else {
    return ScreenRect(
      Rect.fromLTWH(
        screenOrigin.dx,
        screenOrigin.dy,
        localRect.width,
        localRect.height,
      ),
      isMacOS: false,
    );
  }
}