getWidgetPosition function

Offset getWidgetPosition({
  1. GlobalKey<State<StatefulWidget>>? key,
  2. BuildContext? context,
  3. RenderObject? parent,
})

Gets the position of a widget on the screen.

For some reason, using the context generates a position that is at a offset compared to the real position.

Implementation

Offset getWidgetPosition({
  GlobalKey? key,
  BuildContext? context,
  RenderObject? parent,
}) {
  BuildContext ctx = context ?? key!.currentContext!;
  var renderBox = ctx.findRenderObject() as RenderBox;
  return renderBox.localToGlobal(Offset.zero, ancestor: parent);
}