Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flutter, how do I find a widget in some (x,y) position?

Given some (x,y) coordinates, in logical pixels, relative to:

  1. The top-left of the screen.
  2. The top-left of some Container.

How can I know which widget is in that position?

like image 438
MarcG Avatar asked Jul 08 '18 22:07

MarcG


1 Answers

I think you are after the hitTest method in RenderBox class. As per the documentation

Returns true if the given point is contained in this render object or one of its descendants. Adds any render objects that contain the point to the given hit test result.

So you can get the RenderBox instance of the widget from the key context

GlobalKey key = new GlobalKey();
//your stuff
final RenderBox box = key.currentContext.findRenderObject()
// do box.hitTest
like image 74
RameshVel Avatar answered Oct 10 '22 04:10

RameshVel