diff --git a/example/main.dart b/example/main.dart index 4a5260c9e1..d1e3b8de3a 100644 --- a/example/main.dart +++ b/example/main.dart @@ -126,7 +126,7 @@ class _MyHomePageState extends State { decorationColor: Colors.redAccent, decoration: TextDecoration.underline, ), - onLinkTap: (url) { + onLinkTap: (text, url) { print("Opening $url..."); }, onImageTap: (src) { diff --git a/lib/html_parser.dart b/lib/html_parser.dart index b89006af7f..1c09bd19d1 100644 --- a/lib/html_parser.dart +++ b/lib/html_parser.dart @@ -154,8 +154,9 @@ class HtmlOldParser extends StatelessWidget { ), onTap: () { if (node.attributes.containsKey('href') && onLinkTap != null) { + final String text = node.text; String url = node.attributes['href']; - onLinkTap(url); + onLinkTap(text, url); } }); case "abbr": diff --git a/lib/rich_text_parser.dart b/lib/rich_text_parser.dart index 25258e2ae1..090c2efcfc 100644 --- a/lib/rich_text_parser.dart +++ b/lib/rich_text_parser.dart @@ -14,7 +14,7 @@ typedef CustomTextStyle = TextStyle Function( ); typedef CustomTextAlign = TextAlign Function(dom.Element elem); typedef CustomEdgeInsets = EdgeInsets Function(dom.Node node); -typedef OnLinkTap = void Function(String url); +typedef OnLinkTap = void Function(String text, String url); typedef OnImageTap = void Function(String source); const OFFSET_TAGS_FONT_SIZE_FACTOR = @@ -47,7 +47,7 @@ class LinkTextSpan extends TextSpan { children: children ?? [], recognizer: TapGestureRecognizer() ..onTap = () { - onLinkTap?.call(url); + onLinkTap?.call(text, url); }, ); } @@ -60,6 +60,7 @@ class LinkBlock extends Container { final List children; LinkBlock({ + String text, String url, EdgeInsets padding, EdgeInsets margin, @@ -70,7 +71,7 @@ class LinkBlock extends Container { margin: margin, child: GestureDetector( onTap: () { - onLinkTap(url); + onLinkTap(text, url); }, child: Column( children: children, diff --git a/test/html_parser_test.dart b/test/html_parser_test.dart index 44cb709f94..5d377962d4 100644 --- a/test/html_parser_test.dart +++ b/test/html_parser_test.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter_html/flutter_html.dart'; import 'package:flutter_html/rich_text_parser.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:flutter_html/flutter_html.dart'; void main() { testWidgets("Check that default parser does not fail on empty data", @@ -65,6 +65,7 @@ void main() { testWidgets("Check that tapping on the `a` tag calls the callback", (tester) async { String html = "Test link"; + String textTapped; String urlTapped; await tester.pumpWidget( @@ -72,7 +73,8 @@ void main() { home: Scaffold( body: Html( data: html, - onLinkTap: (url) { + onLinkTap: (text, url) { + textTapped = text; urlTapped = url; }, useRichText: false, @@ -81,6 +83,7 @@ void main() { ), ); await tester.tap(find.text("Test link")); + expect(textTapped, "Test link"); expect(urlTapped, "https://github.com"); }); @@ -88,6 +91,7 @@ void main() { "Check that tapping on the `a` tag calls the callback `RichText` parser", (tester) async { String html = "Test link"; + String textTapped; String urlTapped; await tester.pumpWidget( @@ -95,7 +99,8 @@ void main() { home: Scaffold( body: Html( data: html, - onLinkTap: (url) { + onLinkTap: (text, url) { + textTapped = text; urlTapped = url; }, useRichText: true, @@ -104,6 +109,7 @@ void main() { ), ); await tester.tap(find.byType(RichText)); + expect(textTapped, "Test link"); expect(urlTapped, "https://github.com"); }); @@ -168,10 +174,7 @@ void main() { await tester.pumpWidget( MaterialApp( home: Scaffold( - body: Html( - data: "
Address
", - useRichText: false - ), + body: Html(data: "
Address
", useRichText: false), ), ), );