简体   繁体   English

在 Flutter 测试中,是否可以从给定的小部件访问子/父小部件?

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

I'm writing unit and integration tests for Flutter.我正在为 Flutter 编写单元和集成测试。 Is it possible to access children/parent widgets from a given widget?是否可以从给定的小部件访问子/父小部件?

Yes, you can use find.descendant and Element.ancestorWidgetOfExactType .是的,您可以使用find.descendantElement.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);

不推荐使用ancestorWidgetOfExactType,更喜欢使用findAncestorWidgetOfExactType

// Parent with a child & grand child
class ParentWidget extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return ChildWidget(
      child: GrandChildWidget(),
    );
  }
}

// Tests
void main() {
  testWidgets('parent, child & grand-child hierarchy', (WidgetTester tester) async {
    Widget parentWidget = ParentWidget();
    await tester.pumpWidget(parentWidget);
    
    final childFinder = find.descendant(of: find.byWidget(parentWidget), matching: find.byType(ChildWidget));
    expect(childFinder, findsOneWidget);
    
    final grandChildFinder = find.descendant(of: find.byWidget(parentWidget), matching: find.byType(GrandChildWidget));
    expect(grandChildFinder, findsOneWidget);
    
    final parentFinder = find.ancestor(of: find.byWidget(childWidget), matching: find.byType(ParentWidget));
    expect(parentFinder, findsOneWidget);
  });
}

Flutter docs - descendant & ancestor Flutter 文档 - 后代祖先

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:从单个父窗口小部件控制多个子窗口小部件的状态 - Flutter: Controlling the states of multiple child widgets from a single parent widget 如何将子有状态小部件的索引单选按钮值返回到 Flutter 中的另一个有状态小部件 - how to return index radio button value from children Stateful widgets to another stateful widget in Flutter 从 Flutter 中的父小部件访问 const 子小部件变量 - Access const child widget variable from parent widget in Flutter 将子小部件定位在 Column 父小部件的中心和末尾 - Positioning children widgets at the center and at the end of a Column parent widget 在 Flutter 中修改子小部件的静态变量 - Modifying static variable from children widgets in Flutter Flutter:当小部件没有父/子关系时,如何从小部件 B 调用小部件 A 中的 function? - Flutter: How do I call a function in widget A from widget B when the widgets do not have a parent/child relationship? 是否可以在某些子部件中禁用 SelectionArea? Flutter 3.3 - Is it possible to disable SelectionArea in some children widgets? Flutter 3.3 在颤动中从父小部件重置子小部件 - Reset child widget from parent widget in flutter Flutter,访问父容器widget的padding值 - Flutter, Access the padding value of parent container widget 如何在Flutter中访问父级中的子控件的数据? - How to access data of child widget in parent in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM