(译)查找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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Clojure编程

Clojure编程

Chas Emerick、Brian Carper、Christophe Grand / 徐明明、杨寿勋 / 电子工业出版社 / 2013-3-26 / 99.00元

Clojure是一种实用的通用语言,它是传奇语言LISP的方言,可与Ruby、Python等动态语言相媲美,更以无缝Java库、服务,以及拥有JVM系统得天独厚的资源优势而胜出。本书既可以用来熟悉Clojure基础知识与常见例子,也可了解其相关的实践领域与话题,更可以看到这一JVM平台上的LISP如何帮助消除不必要的复杂性,为大家在编程实践中解决最具挑战性的问题开辟新的选择——更具灵活性,更适于W......一起来看看 《Clojure编程》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

UNIX 时间戳转换