Skip to content

Commit a80e429

Browse files
Merge pull request Sub6Resources#15 from loitran/feature_link_tap_updated
Add link tap callback
2 parents 88c3568 + 4f9185c commit a80e429

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,7 @@ until official support is added.
177177
padding: EdgeInsets.all(8.0),
178178
backgroundColor: Colors.white70,
179179
defaultTextStyle: TextStyle(fontFamily: 'serif'),
180+
onLinkTap: (url) {
181+
// open url in a webview
182+
}
180183
)

lib/flutter_html.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import 'package:flutter/material.dart';
44
import 'package:flutter_html/html_parser.dart';
55

66
class Html extends StatelessWidget {
7-
Html(
8-
{Key key,
9-
@required this.data,
10-
this.padding,
11-
this.backgroundColor,
12-
this.defaultTextStyle = const TextStyle(color: Colors.black)})
13-
: super(key: key);
7+
Html({
8+
Key key,
9+
@required this.data,
10+
this.padding,
11+
this.backgroundColor,
12+
this.defaultTextStyle = const TextStyle(color: Colors.black),
13+
this.onLinkTap
14+
}) : super(key: key);
1415

1516
final String data;
1617
final EdgeInsetsGeometry padding;
1718
final Color backgroundColor;
1819
final TextStyle defaultTextStyle;
20+
final Function onLinkTap;
1921

2022
@override
2123
Widget build(BuildContext context) {
@@ -29,7 +31,7 @@ class Html extends StatelessWidget {
2931
style: defaultTextStyle,
3032
child: Wrap(
3133
alignment: WrapAlignment.start,
32-
children: HtmlParser(width: width).parse(data),
34+
children: HtmlParser(width: width, onLinkTap: onLinkTap).parse(data),
3335
),
3436
),
3537
);

lib/html_parser.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import 'package:html/parser.dart' as parser;
33
import 'package:html/dom.dart' as dom;
44

55
class HtmlParser {
6-
HtmlParser({@required this.width});
6+
HtmlParser({
7+
@required this.width,
8+
this.onLinkTap,
9+
});
710

811
final double width;
12+
final Function onLinkTap;
913

1014
static const _supportedElements = [
1115
"a",
@@ -96,14 +100,22 @@ class HtmlParser {
96100
}
97101
switch (node.localName) {
98102
case "a":
99-
return DefaultTextStyle.merge(
100-
child: Wrap(
101-
children: _parseNodeList(node.nodes),
102-
),
103-
style: const TextStyle(
104-
decoration: TextDecoration.underline,
105-
color: Colors.blueAccent,
106-
decorationColor: Colors.blueAccent),
103+
return GestureDetector(
104+
child: DefaultTextStyle.merge(
105+
child: Wrap(
106+
children: _parseNodeList(node.nodes),
107+
),
108+
style: const TextStyle(
109+
decoration: TextDecoration.underline,
110+
color: Colors.blueAccent,
111+
decorationColor: Colors.blueAccent),
112+
),
113+
onTap: () {
114+
if (node.attributes.containsKey('href') && onLinkTap != null) {
115+
String url = node.attributes['href'];
116+
onLinkTap(url);
117+
}
118+
}
107119
);
108120
case "abbr":
109121
return DefaultTextStyle.merge(

0 commit comments

Comments
 (0)