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...
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With