首页 > 解决方案 > 如何在 Flutter 小部件测试中组合两个 Finder?

问题描述

使用package:flutter_test,我可以创建一个查找器,用一个键查找小部件:

expect(find.byKey(const ValueKey('counter')), findsOneWidget);

或通过文字:

expect(find.text('0'), findsOneWidget);

我还可以找到从这个小部件下降的小部件:

expect(
  find.descendant(
    of: find.byKey(const ValueKey('counter')),
    matching: find.text('0'),
  ),
  findsNothing,
);

或者祖先:

expect(
  find.ancestor(
    of: find.text('0'),
    matching: find.byKey(const ValueKey('counter')),
  ),
  findsNothing,
);

但是我如何结合这些查找器来验证是否有一个带有“计数器”键和“0”作为文本的小部件?例如:

Text(
  '$_counter',
  key: const Key('counter'),
  style: Theme.of(context).textTheme.headline4,
),

标签: flutterflutter-integration-test

解决方案


推荐阅读