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

后端 未结 1 1475
臣服心动
臣服心动 2021-02-05 16:20

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

相关标签:
1条回答
  • 2021-02-05 16:40

    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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题