Abhay V Ashokan
Abhay V Ashokan

Reputation: 392

How to get the position of a widget in the screen (preferably in Offset)?

I am trying to implement Match-The-Following. The approach I could come up with is that the onPanStart and onPanEnd to get the starting Offset and ending Offset. I also need to get the Offset of the widget on the screen to match.

Is there any method to get the local position of widget on the screen (Widget is a Container) ?

Upvotes: 3

Views: 3778

Answers (1)

Benedikt J Schlegel
Benedikt J Schlegel

Reputation: 1939

I think this is what you are looking for:

Set a key for the Widget:

Container(
           key: _key,
           color: Colors.red,
         ),

And get the position of the widget like this:

 final RenderBox renderBox = _key.currentContext.findRenderObject();
    final position = renderBox.localToGlobal(Offset.zero);

Upvotes: 3

Related Questions