With a Flutter WidgetTester how can you tap on a TextSpan, like in the code below?
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With