gpt4 book ai didi

flutter - 查找 TextSpan 以使用 Flutter 测试

转载 作者:行者123 更新时间:2023-12-03 16:02:55 29 4
gpt4 key购买 nike

使用 Flutter WidgetTester 如何点击 TextSpan,如下面的代码所示?

RichText(
text: TextSpan(
children: [
TextSpan(text: 'aaa '),
TextSpan(
text: 'bbb ',
recognizer: TapGestureRecognizer()
..onTap = () {
// How to reach this code in a widget test?
},
),
TextSpan(text: 'ccc'),
],
),
)

最佳答案

CommonFinders byWidgetPredicate method

InlineSpan visitChildren method

找到 TextSpan:

final finder = find.byWidgetPredicate(
(widget) => widget is RichText && tapTextSpan(widget, "bbb "),
);
bool findTextAndTap(InlineSpan visitor, String text) {
if (visitor is TextSpan && visitor.text == text) {
(visitor.recognizer as TapGestureRecognizer).onTap();

return false;
}

return true;
}

bool tapTextSpan(RichText richText, String text) {
final isTapped = !richText.text.visitChildren(
(visitor) => findTextAndTap(visitor, text),
);

return isTapped;
}

关于flutter - 查找 TextSpan 以使用 Flutter 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60247342/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com