Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

How can I simulate tap event on a Flutter widget?

For example, how do I simulate tapping on a Tabbar title? And more importantly, how can I find the widget in the first place? Please note that I don't want to call the relevant callbacks, nor testing widgets using Flutter test classes, but I'd like to simulate the tapping on widgets at runtime instead.

EDIT 1: (Please Note) I DO NOT want to test a widget or call a method that is assigned to a GestureDetector.onTap or RaisedButton.onPressed etc...

like image 513
NET3 Avatar asked Feb 11 '19 23:02

NET3


1 Answers

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);
  }
}
like image 77
Rémi Rousselet Avatar answered Sep 21 '22 12:09

Rémi Rousselet