有没有一种简单的方法可以在 Flutter 测试中找到从 RichText 构建的特定文本?

2023-12-31

例如,我可能有一个RichText在当前的小部件树中,看起来像

RichText(
 text: TextSpan(
   text: 'Hello ',
   style: DefaultTextStyle.of(context).style,
   children: <TextSpan>[
     TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
     TextSpan(text: ' world!'),
   ],
 ),
)

我尝试使用find.text('Hello bold world!')但它不起作用,因为它不是文本。


框架解决方案

我最近有贡献了 https://github.com/flutter/flutter/pull/87197此功能适用于 Flutter 框架,即内置查找器。

find.text()

您现在可以启用findRichText范围 https://github.com/creativecreatorormaybenot/flutter/blob/d78390df3742b7d84f56d533075f4b76fc4b6890/packages/flutter_test/lib/src/finders.dart#L58,然后也会找到独立的RichText小部件:

find.text(
  'Hello bold world!',
  findRichText: true,
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有一种简单的方法可以在 Flutter 测试中找到从 RichText 构建的特定文本? 的相关文章

随机推荐