diff --git a/lib/flutter_html.dart b/lib/flutter_html.dart index f57aadc167..1d5b827f96 100644 --- a/lib/flutter_html.dart +++ b/lib/flutter_html.dart @@ -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, @@ -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) { @@ -52,6 +56,8 @@ class Html extends StatelessWidget { width: width, onLinkTap: onLinkTap, renderNewlines: renderNewlines, + customEdgeInsets: customEdgeInsets, + customTextStyle: customTextStyle, html: data, onImageError: onImageError, linkStyle: linkStyle, diff --git a/lib/html_parser.dart b/lib/html_parser.dart index 2555a92e21..201c1c2938 100644 --- a/lib/html_parser.dart +++ b/lib/html_parser.dart @@ -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 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 = @@ -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; @@ -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; @@ -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; } @@ -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 @@ -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(