Skip to content

Instantly share code, notes, and snippets.

@vishna
Created February 3, 2020 14:31
Show Gist options
  • Save vishna/d27c6a5fee14e059d9c7759727630568 to your computer and use it in GitHub Desktop.
Save vishna/d27c6a5fee14e059d9c7759727630568 to your computer and use it in GitHub Desktop.
WidgetTester RichEdit Extension - find a piece of text, e.g. tap a link in the text
/// found nothing better than this
/// https://github.com/flutter/flutter/blob/master/packages/flutter/test/widgets/hyperlink_test.dart#L47
/// sooo...
extension WidgetTesterRichEditExtension on WidgetTester {
Iterable<InlineSpan> findTextSpans(
Finder richTextFinder, bool Function(InlineSpan) predicate) {
final richText = widget<RichText>(richTextFinder);
expect(richText.text, isInstanceOf<TextSpan>());
final TextSpan innerText = richText.text;
return innerText.children.where(predicate);
}
}
/// then
void findALinkAnTapIt(WidgetTester tester) async {
final richTextFinder = find.byKey(ValueKey("myKey"));
final spans = tester.findTextSpans(
richTextFinder,
(inlineSpan) =>
inlineSpan is TextSpan &&
inlineSpan.recognizer is TapGestureRecognizer);
final TextSpan = spans.first;
expect(spans, hasLength(1));
final TextSpan linkSpan = spans.first;
final TapGestureRecognizer recognizer = linkSpan.recognizer;
recognizer.onTap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment