在Flutter测试中是否可以从给定的小部件访问子级/父级小部件? [英] Is it possible to access children/parent widgets from a given widget, in a Flutter test?
本文介绍了在Flutter测试中是否可以从给定的小部件访问子级/父级小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为Flutter编写单元和集成测试。
解决方案
是的,您可以使用 find.descendant
和 Element.ancestorWidgetOfExactType
。。 p>
示例:
//查找一个后代(子代)的RichText小部件(子代) / brandb // grand-child / etc)带有文本 Tab 1的选项卡小部件
find.descendant(of:find.text('Tab 1')),匹配项:find.byType(RichText ));
//查找类型为MyParentWidget的父窗口小部件。
tester.element(find.byType(MyChildWidget))
.ancestorWidgetOfExactType(MyParentWidget);
I'm writing unit and integration tests for Flutter. Is it possible to access children/parent widgets from a given widget?
解决方案
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);
这篇关于在Flutter测试中是否可以从给定的小部件访问子级/父级小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文