Is it possible to access children/parent widgets from a given widget, in a Flutter test?

[亡魂溺海] 提交于 2020-01-14 07:03:37

问题


I'm writing unit and integration tests for Flutter. Is it possible to access children/parent widgets from a given widget?


回答1:


Yes, you can use find.descendant and Element.ancestorWidgetOfExactType.

Examples:

// Finds a RichText widget that a descendant (child/grand-child/etc) of a
// tab widget with text "Tab 1"
find.descendant(of: find.text('Tab 1'), matching: find.byType(RichText));

// Finds a parent widget of type MyParentWidget.
tester.element(find.byType(MyChildWidget))
  .ancestorWidgetOfExactType(MyParentWidget);


来源:https://stackoverflow.com/questions/47296080/is-it-possible-to-access-children-parent-widgets-from-a-given-widget-in-a-flutt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!