(译)查找widget

栏目: 编程工具 · 发布时间: 5年前

内容简介:为了在测试环境中定位Widget,我们需要使用在这个配方中,我们将查看

原文链接

为了在测试环境中定位Widget,我们需要使用 Finder 类。虽然可以编写我们自己的 Finder 类,但使用 flutter_test 包提供的 工具 定位Widgets通常更方便。

在这个配方中,我们将查看 flutter_test 包提供的 find 常量,并演示如何使用它提供的一些Finder。有关可用查找器的完整列表,请参阅 CommonFinders文档

如果您不熟悉Widget测试和 Finder 类的角色,请查看Widget测试简介。

路径

  1. 查找一个文本Widget
  2. 查找带有特定key的Widget
  3. 查找一个特定的widget实例

1.查找一个文本widget

在我们的测试中,我们经常需要找到包含特定文本的widget。这正是find.text方法的用途。它将创建一个Finder,用于搜索显示特定文本字符串的widget。

testWidgets('finds a Text Widget', (WidgetTester tester) async {
  // Build an App with a Text Widget that displays the letter 'H'
  await tester.pumpWidget(MaterialApp(
    home: Scaffold(
      body: Text('H'),
    ),
  ));

  // Find a Widget that displays the letter 'H'
  expect(find.text('H'), findsOneWidget);
});

2.查找带有特定key的Widget

在某些情况下,我们可能希望根据已提供给它的Key找到一个Widget。如果我们显示相同Widget的多个实例,这可能很方便。例如,我们可能有一个ListView,它显示包含相同文本的多个Text Widgets。

在这种情况下,我们可以为列表中的每个Widget提供一个Key。这将允许我们唯一地标识特定的Widget,从而更容易在测试环境中找到Widget。

testWidgets('finds a Widget using a Key', (WidgetTester tester) async {
  // Define our test key
  final testKey = Key('K');

  // Build a MaterialApp with the testKey
  await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));

  // Find the MaterialApp Widget using the testKey
  expect(find.byKey(testKey), findsOneWidget);
});

3.查找一个特定的widget实例

最后,我们可能对定位特定的Widget实例感兴趣。例如,在创建带有 child 属性的Widget时候,并且我们想要确保我们渲染了子Widget时,这可能很有用。

testWidgets('finds a specific instance', (WidgetTester tester) async {
  final childWidget = Padding(padding: EdgeInsets.zero);

  // Provide our childWidget to the Container
  await tester.pumpWidget(Container(child: childWidget));

  // Search for the childWidget in the tree and verify it exists
  expect(find.byWidget(childWidget), findsOneWidget);
});

总结

flutter_test 包提供的 find 常量为我们提供了几种在测试环境中定位Widget的方法。该配方展示了这些方法中的三种,并且存在多种用于不同目的的方法中。

如果上述示例不适用于特定用例,请参阅 CommonFinders文档 以查看所有可用方法。

完整代码

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('finds a Text Widget', (WidgetTester tester) async {
    // Build an App with a Text Widget that displays the letter 'H'
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Text('H'),
      ),
    ));

    // Find a Widget that displays the letter 'H'
    expect(find.text('H'), findsOneWidget);
  });

  testWidgets('finds a Widget using a Key', (WidgetTester tester) async {
    // Define our test key
    final testKey = Key('K');

    // Build a MaterialApp with the testKey
    await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));

    // Find the MaterialApp Widget using the testKey
    expect(find.byKey(testKey), findsOneWidget);
  });

  testWidgets('finds a specific instance', (WidgetTester tester) async {
    final childWidget = Padding(padding: EdgeInsets.zero);

    // Provide our childWidget to the Container
    await tester.pumpWidget(Container(child: childWidget));

    // Search for the childWidget in the tree and verify it exists
    expect(find.byWidget(childWidget), findsOneWidget);
  });
}

以上所述就是小编给大家介绍的《(译)查找widget》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

用户体验的要素

用户体验的要素

Jesse James Garrett / 范晓燕 / 机械工业出版社 / 2007年10月 / 25.00

这不是一本关于“怎样做(How-to)”的书。有很多很多讨论如何建设网站的书,这本不是。 这不是一本关于技术的书。在这里你找不到一行代码。 这不是一本有答案的书。相反,这本书说的是“如何提出正确的问题”。 这本书将告诉你,在你阅读其他书籍的之前,你需要提前了解什么。如果你需要一个大的概念,如果你需要了解用户体验设计师所做出的决策的环境,这本书很适合你。 这本书经过精心设计,......一起来看看 《用户体验的要素》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具