(译)查找widget

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

内容简介:为了在测试环境中定位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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

数据压缩导论(第4版)

数据压缩导论(第4版)

[美] Khalid Sayood / 贾洪峰 / 人民邮电出版社 / 2014-1 / 129.00

数据压缩已经成为信息革命的一门支撑技术,这场革命已经改变了我们的生活,而在此过程中,数据压缩也变得几乎无处不在。从MP3播放器到智能手机,再到数字电视和数字电影,数据压缩几乎成了所有信息技术的必备要素。 近年来,以大数据为标志的互联网技术高歌猛进。数据规模大、产生速度快、来源多样等特性,导致数据存储和处理都前所未有地复杂。《数据压缩导论(第4版)》作为迄今为止数据压缩领域最全面而深入的著作,......一起来看看 《数据压缩导论(第4版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具