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

Added custom textstyle and edgeinsets callback #72

Merged
merged 2 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -13,6 +13,8 @@ class Html extends StatelessWidget {
this.onLinkTap,
this.renderNewlines = false,
this.customRender,
this.customEdgeInsets,
this.customTextStyle,
this.blockSpacing = 14.0,
this.useRichText = false,
}) : super(key: key);
Expand All @@ -29,6 +31,8 @@ class Html extends StatelessWidget {
/// Either return a custom widget for specific node types or return null to
/// fallback to the default rendering.
final CustomRender customRender;
final CustomEdgeInsets customEdgeInsets;
final CustomTextStyle customTextStyle;

@override
Widget build(BuildContext context) {
Expand All @@ -45,6 +49,8 @@ class Html extends StatelessWidget {
width: width,
onLinkTap: onLinkTap,
renderNewlines: renderNewlines,
customEdgeInsets: customEdgeInsets,
customTextStyle: customTextStyle,
html: data,
)
: HtmlOldParser(
Expand Down
25 changes: 21 additions & 4 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart' as dom;

typedef CustomRender = Widget Function(dom.Node node, List<Widget> children);
typedef CustomTextStyle = TextStyle Function(dom.Node node, TextStyle baseStyle);
typedef CustomEdgeInsets = EdgeInsets Function(dom.Node node);
typedef OnLinkTap = void Function(String url);

const OFFSET_TAGS_FONT_SIZE_FACTOR =
Expand Down Expand Up @@ -144,6 +146,8 @@ class HtmlRichTextParser extends StatelessWidget {
this.onLinkTap,
this.renderNewlines = false,
this.html,
this.customTextStyle,
this.customEdgeInsets,
});

final double indentSize = 10.0;
Expand All @@ -152,6 +156,8 @@ class HtmlRichTextParser extends StatelessWidget {
final onLinkTap;
final bool renderNewlines;
final String html;
final CustomTextStyle customTextStyle;
final CustomEdgeInsets customEdgeInsets;

// style elements set a default style
// for all child nodes
Expand Down Expand Up @@ -472,6 +478,14 @@ class HtmlRichTextParser extends StatelessWidget {
nextContext.blockType = 'blockquote';
break;
}

if (customTextStyle != null) {
final TextStyle customStyle = customTextStyle(node, childStyle);
if (customStyle != null) {
childStyle = customStyle;
}
}

nextContext.childStyle = childStyle;
}

Expand Down Expand Up @@ -610,6 +624,11 @@ class HtmlRichTextParser extends StatelessWidget {
parseContext.parentElement = null;
TextAlign textAlign = TextAlign.left;

EdgeInsets _customEdgeInsets;
if (customEdgeInsets != null) {
_customEdgeInsets = customEdgeInsets(node);
}

switch (node.localName) {
case "hr":
parseContext.rootWidgetList
Expand Down Expand Up @@ -715,10 +734,8 @@ class HtmlRichTextParser extends StatelessWidget {
));
}
BlockText blockText = BlockText(
margin: EdgeInsets.only(
top: 8.0,
bottom: 8.0,
left: parseContext.indentLevel * indentSize),
margin: _customEdgeInsets ??
EdgeInsets.only(top: 8.0, bottom: 8.0, left: parseContext.indentLevel * indentSize),
padding: EdgeInsets.all(2.0),
decoration: decoration,
child: RichText(
Expand Down