WidgetPosition class

Utility for finding widget positions in screen coordinates.

Since palettes run in separate Flutter engines, each palette has its own coordinate system. This class helps convert local widget positions to screen coordinates that can be used for positioning other palettes.

Platform differences (macOS vs Windows coordinate systems) are handled automatically.

Usage in a Palette

class EditorPalette extends StatefulWidget {
  @override
  State<EditorPalette> createState() => _EditorPaletteState();
}

class _EditorPaletteState extends State<EditorPalette> {
  final _textFieldKey = GlobalKey();
  final _positionHelper = WidgetPosition();

  void _onSlashTyped() async {
    // Set window bounds first
    _positionHelper.windowBounds = await PaletteSelf.screenRect;

    // Get text field's screen position
    final textFieldRect = _positionHelper.getWidgetRect(_textFieldKey);
    if (textFieldRect != null) {
      // Send position to host for slash menu placement
      PaletteMessenger.send('show-slash-menu', {
        'x': textFieldRect.bottomLeft.dx,
        'y': textFieldRect.bottomLeft.dy,
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextField(key: _textFieldKey, ...);
  }
}

Constructors

WidgetPosition({bool? isMacOS})

Properties

hashCode int
The hash code for this object.
no setterinherited
isMacOS bool
Whether we're on macOS (affects coordinate conversion). Detected automatically from platform.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
windowBounds ScreenRect?
The palette's window bounds in screen coordinates.
getter/setter pair

Methods

getLocalRect(GlobalKey<State<StatefulWidget>> key) Rect?
Get a widget's local bounds (relative to the Flutter view).
getPointInWidget(GlobalKey<State<StatefulWidget>> key, Offset localOffset, {ScreenRect? windowBounds}) Offset?
Get the screen position of a point within a widget.
getWidgetAnchor(GlobalKey<State<StatefulWidget>> key, Anchor anchor, {ScreenRect? windowBounds}) Offset?
Get a specific anchor point of a widget in screen coordinates.
getWidgetRect(GlobalKey<State<StatefulWidget>> key, {ScreenRect? windowBounds}) ScreenRect?
Get a widget's bounds in screen coordinates.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited