Skip to content

Commit

Permalink
Merge pull request #15 from loitran/feature_link_tap_updated
Browse files Browse the repository at this point in the history
Add link tap callback
  • Loading branch information
Sub6Resources authored Sep 1, 2018
2 parents 88c3568 + 4f9185c commit a80e429
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@ until official support is added.
padding: EdgeInsets.all(8.0),
backgroundColor: Colors.white70,
defaultTextStyle: TextStyle(fontFamily: 'serif'),
onLinkTap: (url) {
// open url in a webview
}
)
18 changes: 10 additions & 8 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import 'package:flutter/material.dart';
import 'package:flutter_html/html_parser.dart';

class Html extends StatelessWidget {
Html(
{Key key,
@required this.data,
this.padding,
this.backgroundColor,
this.defaultTextStyle = const TextStyle(color: Colors.black)})
: super(key: key);
Html({
Key key,
@required this.data,
this.padding,
this.backgroundColor,
this.defaultTextStyle = const TextStyle(color: Colors.black),
this.onLinkTap
}) : super(key: key);

final String data;
final EdgeInsetsGeometry padding;
final Color backgroundColor;
final TextStyle defaultTextStyle;
final Function onLinkTap;

@override
Widget build(BuildContext context) {
Expand All @@ -29,7 +31,7 @@ class Html extends StatelessWidget {
style: defaultTextStyle,
child: Wrap(
alignment: WrapAlignment.start,
children: HtmlParser(width: width).parse(data),
children: HtmlParser(width: width, onLinkTap: onLinkTap).parse(data),
),
),
);
Expand Down
30 changes: 21 additions & 9 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart' as dom;

class HtmlParser {
HtmlParser({@required this.width});
HtmlParser({
@required this.width,
this.onLinkTap,
});

final double width;
final Function onLinkTap;

static const _supportedElements = [
"a",
Expand Down Expand Up @@ -96,14 +100,22 @@ class HtmlParser {
}
switch (node.localName) {
case "a":
return DefaultTextStyle.merge(
child: Wrap(
children: _parseNodeList(node.nodes),
),
style: const TextStyle(
decoration: TextDecoration.underline,
color: Colors.blueAccent,
decorationColor: Colors.blueAccent),
return GestureDetector(
child: DefaultTextStyle.merge(
child: Wrap(
children: _parseNodeList(node.nodes),
),
style: const TextStyle(
decoration: TextDecoration.underline,
color: Colors.blueAccent,
decorationColor: Colors.blueAccent),
),
onTap: () {
if (node.attributes.containsKey('href') && onLinkTap != null) {
String url = node.attributes['href'];
onLinkTap(url);
}
}
);
case "abbr":
return DefaultTextStyle.merge(
Expand Down

0 comments on commit a80e429

Please sign in to comment.