Skip to content

Commit fdd8c29

Browse files
Merge pull request Sub6Resources#72 from LucasR93/master
Added custom textstyle and edgeinsets callback
2 parents 1169d15 + 1e427df commit fdd8c29

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/flutter_html.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Html extends StatelessWidget {
1313
this.onLinkTap,
1414
this.renderNewlines = false,
1515
this.customRender,
16+
this.customEdgeInsets,
17+
this.customTextStyle,
1618
this.blockSpacing = 14.0,
1719
this.useRichText = false,
1820
this.onImageError,
@@ -36,6 +38,8 @@ class Html extends StatelessWidget {
3638
/// Either return a custom widget for specific node types or return null to
3739
/// fallback to the default rendering.
3840
final CustomRender customRender;
41+
final CustomEdgeInsets customEdgeInsets;
42+
final CustomTextStyle customTextStyle;
3943

4044
@override
4145
Widget build(BuildContext context) {
@@ -52,6 +56,8 @@ class Html extends StatelessWidget {
5256
width: width,
5357
onLinkTap: onLinkTap,
5458
renderNewlines: renderNewlines,
59+
customEdgeInsets: customEdgeInsets,
60+
customTextStyle: customTextStyle,
5561
html: data,
5662
onImageError: onImageError,
5763
linkStyle: linkStyle,

lib/html_parser.dart

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:html/dom.dart' as dom;
66
import 'package:html/parser.dart' as parser;
77

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

1113
const OFFSET_TAGS_FONT_SIZE_FACTOR =
@@ -144,11 +146,14 @@ class HtmlRichTextParser extends StatelessWidget {
144146
this.onLinkTap,
145147
this.renderNewlines = false,
146148
this.html,
149+
this.customTextStyle,
150+
this.customEdgeInsets,
147151
this.onImageError,
148152
this.linkStyle = const TextStyle(
149153
decoration: TextDecoration.underline,
150154
color: Colors.blueAccent,
151-
decorationColor: Colors.blueAccent),
155+
decorationColor: Colors.blueAccent,
156+
),
152157
});
153158

154159
final double indentSize = 10.0;
@@ -157,6 +162,8 @@ class HtmlRichTextParser extends StatelessWidget {
157162
final onLinkTap;
158163
final bool renderNewlines;
159164
final String html;
165+
final CustomTextStyle customTextStyle;
166+
final CustomEdgeInsets customEdgeInsets;
160167
final ImageErrorListener onImageError;
161168
final TextStyle linkStyle;
162169

@@ -485,6 +492,14 @@ class HtmlRichTextParser extends StatelessWidget {
485492
//No additional styles
486493
break;
487494
}
495+
496+
if (customTextStyle != null) {
497+
final TextStyle customStyle = customTextStyle(node, childStyle);
498+
if (customStyle != null) {
499+
childStyle = customStyle;
500+
}
501+
}
502+
488503
nextContext.childStyle = childStyle;
489504
}
490505

@@ -619,6 +634,11 @@ class HtmlRichTextParser extends StatelessWidget {
619634
parseContext.parentElement = null;
620635
TextAlign textAlign = TextAlign.left;
621636

637+
EdgeInsets _customEdgeInsets;
638+
if (customEdgeInsets != null) {
639+
_customEdgeInsets = customEdgeInsets(node);
640+
}
641+
622642
switch (node.localName) {
623643
case "hr":
624644
parseContext.rootWidgetList
@@ -738,10 +758,8 @@ class HtmlRichTextParser extends StatelessWidget {
738758
));
739759
}
740760
BlockText blockText = BlockText(
741-
margin: EdgeInsets.only(
742-
top: 8.0,
743-
bottom: 8.0,
744-
left: parseContext.indentLevel * indentSize),
761+
margin: _customEdgeInsets ??
762+
EdgeInsets.only(top: 8.0, bottom: 8.0, left: parseContext.indentLevel * indentSize),
745763
padding: EdgeInsets.all(2.0),
746764
decoration: decoration,
747765
child: RichText(

0 commit comments

Comments
 (0)