How can I simulate a tap event on a Flutter widget?

First, obtain a RenderBox. Then just call hitTest method. Any will do, as long as it’s mounted in the tree.

To do so you’ll have to use BuildContext through context.findRenderObject().

BuildContext context;

final renderObj = context.findRenderObject();
if (renderObj is RenderBox) {
  final hitTestResult = HitTestResult();
  if (renderObj.hitTest(hitTestResult, position:  /* The offset where you want to "tap" */)) {
    // a descendant of `renderObj` got tapped
    print(hitTestResult.path);
  }
}

Leave a Comment