Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to pass in callback function for link clicks #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,7 @@ until official support is added.
padding: EdgeInsets.all(8.0),
backgroundColor: Colors.white70,
defaultTextStyle: TextStyle(color: Colors.black),
onLinkTap: (url) {
// open url in a webview
}
)
6 changes: 4 additions & 2 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class Html extends StatelessWidget {
@required this.data,
this.padding,
this.backgroundColor,
this.defaultTextStyle = const TextStyle(color: Colors.black)})
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 @@ -24,7 +26,7 @@ class Html extends StatelessWidget {
color: backgroundColor,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: HtmlParser(defaultTextStyle: defaultTextStyle).parse(data),
children: HtmlParser(defaultTextStyle: defaultTextStyle, onLinkTap: onLinkTap).parse(data),
),
);
}
Expand Down
49 changes: 32 additions & 17 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({this.defaultTextStyle = const TextStyle(color: Colors.black)});
HtmlParser({
this.defaultTextStyle = const TextStyle(color: Colors.black),
this.onLinkTap
});

final TextStyle defaultTextStyle;
final Function onLinkTap;

static const _supportedElements = [
"a",
Expand Down Expand Up @@ -92,18 +96,27 @@ class HtmlParser {
}
switch (node.localName) {
case "a":
return RichText(
return GestureDetector(
child: RichText(
text: TextSpan(
children: [
TextSpan(
children: _parseInlineElement(node),
style: TextStyle(
decoration: TextDecoration.underline,
),
)
],
style: defaultTextStyle,
));
children: [
TextSpan(
children: _parseInlineElement(node),
style: TextStyle(
decoration: TextDecoration.underline,
),
)
],
style: defaultTextStyle,
),
),
onTap: () {
if (node.attributes.containsKey('href')) {
String url = node.attributes['href'];
onLinkTap(url);
}
}
);
case "abbr":
return RichText(
text: TextSpan(
Expand Down Expand Up @@ -440,11 +453,13 @@ class HtmlParser {
crossAxisAlignment: CrossAxisAlignment.start,
);
case "p":
return RichText(
text: TextSpan(
children: _parseInlineElement(node),
style: defaultTextStyle,
));
return Padding(
padding: EdgeInsets.only(top: 14.0, bottom: 14.0),
child: Column(
children: _parseNodeList(node.nodes),
crossAxisAlignment: CrossAxisAlignment.start,
),
);
case "pre":
return Padding(
padding: const EdgeInsets.only(top: 14.0, bottom: 14.0),
Expand Down