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 more optional stylings #23

Merged
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
6 changes: 6 additions & 0 deletions lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Html extends StatelessWidget {
this.defaultTextStyle = const TextStyle(color: Colors.black),
this.onLinkTap,
this.renderNewlines = false,
this.customRender,
}) : super(key: key);

final String data;
Expand All @@ -21,6 +22,10 @@ class Html extends StatelessWidget {
final Function onLinkTap;
final bool renderNewlines;

/// Either return a custom widget for specific node types or return null to
/// fallback to the default rendering.
final CustomRender customRender;

@override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
Expand All @@ -37,6 +42,7 @@ class Html extends StatelessWidget {
width: width,
onLinkTap: onLinkTap,
renderNewlines: renderNewlines,
customRender: customRender,
).parse(data),
),
),
Expand Down
28 changes: 25 additions & 3 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import 'package:flutter/material.dart';
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart' as dom;

typedef CustomRender = Widget Function(dom.Node node, List<Widget> children);

class HtmlParser {
HtmlParser({
@required this.width,
this.onLinkTap,
this.renderNewlines = false,
this.customRender,
});

final double width;
final Function onLinkTap;
final bool renderNewlines;
final CustomRender customRender;

static const _supportedElements = [
"a",
Expand Down Expand Up @@ -93,21 +97,39 @@ class HtmlParser {
List<Widget> widgetList = new List<Widget>();

if (renderNewlines) {
print("Before: $data");
assert(() {
print("Before: $data");
return true;
}());
data = data.replaceAll("\n", "<br />");
print("After: $data");
assert(() {
print("After: $data");
}());
}
dom.Document document = parser.parse(data);
widgetList.add(_parseNode(document.body));
return widgetList;
}

Widget _parseNode(dom.Node node) {
if (customRender != null) {
final Widget customWidget =
customRender(node, _parseNodeList(node.nodes));
if (customWidget != null) {
return customWidget;
}
}

if (node is dom.Element) {
print("Found ${node.localName}");
assert(() {
print("Found ${node.localName}");
return true;
}());

if (!_supportedElements.contains(node.localName)) {
return Container();
}

switch (node.localName) {
case "a":
return GestureDetector(
Expand Down