Search code examples
flutterflutter-layout

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) ?


Solution

  • 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);