找到一个 TextSpan 来点击 Flutter 测试
Finding a TextSpan to tap on with Flutter tests
使用 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 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;
}