如何在Flutter小部件上模拟点击事件?
例如,如何模拟在Tabbar标题上的点击?更重要的是,我如何首先找到小部件?请注意,我不想调用相关的回调,也不想使用Flutter测试类来测试窗口小部件,但我想在运行时模拟对窗口小部件的轻敲。
编辑1 :(请注意) 我不想测试小部件或调用分配给GestureDetector.onTap或RaisedButton.onPressed等的方法...
答案 0 :(得分:2)
首先,获取一个RenderBox
。然后只需调用hitTest
方法。只要安装在树上,任何人都可以。
为此,您必须使用BuildContext
至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);
}
}