Skip to content

Commit

Permalink
Merge pull request #72 from LucasR93/master
Browse files Browse the repository at this point in the history
Added custom textstyle and edgeinsets callback
  • Loading branch information
Sub6Resources committed May 13, 2019
2 parents 1169d15 + 1e427df commit fdd8c29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
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,
this.onImageError,
Expand All @@ -36,6 +38,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 @@ -52,6 +56,8 @@ class Html extends StatelessWidget {
width: width,
onLinkTap: onLinkTap,
renderNewlines: renderNewlines,
customEdgeInsets: customEdgeInsets,
customTextStyle: customTextStyle,
html: data,
onImageError: onImageError,
linkStyle: linkStyle,
Expand Down
28 changes: 23 additions & 5 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;

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,11 +146,14 @@ class HtmlRichTextParser extends StatelessWidget {
this.onLinkTap,
this.renderNewlines = false,
this.html,
this.customTextStyle,
this.customEdgeInsets,
this.onImageError,
this.linkStyle = const TextStyle(
decoration: TextDecoration.underline,
color: Colors.blueAccent,
decorationColor: Colors.blueAccent),
decorationColor: Colors.blueAccent,
),
});

final double indentSize = 10.0;
Expand All @@ -157,6 +162,8 @@ class HtmlRichTextParser extends StatelessWidget {
final onLinkTap;
final bool renderNewlines;
final String html;
final CustomTextStyle customTextStyle;
final CustomEdgeInsets customEdgeInsets;
final ImageErrorListener onImageError;
final TextStyle linkStyle;

Expand Down Expand Up @@ -485,6 +492,14 @@ class HtmlRichTextParser extends StatelessWidget {
//No additional styles
break;
}

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

nextContext.childStyle = childStyle;
}

Expand Down Expand Up @@ -619,6 +634,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 @@ -738,10 +758,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

0 comments on commit fdd8c29

Please sign in to comment.