微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 测试小部件 find.byType(TextField) 有效, find.byWidget(TextField()) 无效为什么?

如何解决Flutter 测试小部件 find.byType(TextField) 有效, find.byWidget(TextField()) 无效为什么?

我刚刚开始进行颤振小部件测试并遇到了这个问题。我想知道这是否是预期的行为。 当尝试使用 find.byType 找到我的 TextField 时,它会成功,但使用 find.byWidget 却没有。这是正常的还是我做错了什么?目标是稍后在文本字段中输入文本,然后点击按钮。

我的文本字段和按钮:

Column(
    children: [
      TextField(
        style: TextStyle(
          color: Colors.black,),autofocus: true,autocorrect: false,enableSuggestions: false,controller: _controller,cursorColor: Colors.black,decoration: Inputdecoration(
            hintText: 'Fill in.....',hintStyle: TextStyle(fontSize: 18),Row(
        mainAxisAlignment: MainAxisAlignment.end,children: [
          ElevatedButton(
              onpressed: () {
                onSubmit(_controller.text);
              },child: Text('Press me!',style: TextStyle(
                    fontSize: 18,color: Theme.of(context).primaryColorBrightness ==
                        Brightness.dark
                        ? Colors.white
                        : Colors.black,))),],

这是我的测试:

tester.ensureVisible(find.byType(TextInputWidget));
  expect(find.byType(TextInputWidget),findsOneWidget);

  final a = TextField(
    style: TextStyle(
      color: Colors.black,controller: TextEditingController(),decoration: Inputdecoration(
      hintText: 'Fill in.....',);

  //expect(find.byWidget(a),findsOneWidget); // Fails
  expect(find.byType(TextField),findsOneWidget); //Succeeds

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。