From ca44fd798c914c67ac8ac0eeb3eda95316b4f907 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 18 Jan 2022 15:03:54 -0800 Subject: [PATCH] Show tooltip for links --- lib/third_party/linkify/flutter_linkify.dart | 45 ++++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/third_party/linkify/flutter_linkify.dart b/lib/third_party/linkify/flutter_linkify.dart index 9be50666..acf82588 100644 --- a/lib/third_party/linkify/flutter_linkify.dart +++ b/lib/third_party/linkify/flutter_linkify.dart @@ -353,20 +353,24 @@ TextSpan buildTextSpan( (element) { if (element is LinkableElement) { if (useMouseRegion) { - return LinkableSpan( - mouseCursor: SystemMouseCursors.click, - inlineSpan: TextSpan( - text: element.text, - style: linkStyle, - recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null, - ), - ); + return TooltipSpan( + message: element.url, + inlineSpan: LinkableSpan( + mouseCursor: SystemMouseCursors.click, + inlineSpan: TextSpan( + text: element.text, + style: linkStyle, + recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null, + ), + )); } else { - return TextSpan( - text: element.text, - style: linkStyle, - recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null, - ); + return TooltipSpan( + message: element.url, + inlineSpan: TextSpan( + text: element.text, + style: linkStyle, + recognizer: onOpen != null ? (TapGestureRecognizer()..onTap = () => onOpen(element)) : null, + )); } } else { return TextSpan( @@ -378,3 +382,18 @@ TextSpan buildTextSpan( ).toList(), ); } + +// Show a tooltip over an inlined element in a Rich Text widget. +class TooltipSpan extends WidgetSpan { + TooltipSpan({ + required String message, + required InlineSpan inlineSpan, + }) : super( + child: Tooltip( + message: message, + child: Text.rich( + inlineSpan, + ), + ), + ); +}