Skip to content

Commit af77caa

Browse files
committed
Added custom textstyle and edgeinsets callback
1 parent d354604 commit af77caa

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
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
}) : super(key: key);
@@ -29,6 +31,8 @@ class Html extends StatelessWidget {
2931
/// Either return a custom widget for specific node types or return null to
3032
/// fallback to the default rendering.
3133
final CustomRender customRender;
34+
final CustomEdgeInsets customEdgeInsets;
35+
final CustomTextStyle customTextStyle;
3236

3337
@override
3438
Widget build(BuildContext context) {
@@ -45,6 +49,8 @@ class Html extends StatelessWidget {
4549
width: width,
4650
onLinkTap: onLinkTap,
4751
renderNewlines: renderNewlines,
52+
customEdgeInsets: customEdgeInsets,
53+
customTextStyle: customTextStyle,
4854
html: data,
4955
)
5056
: HtmlOldParser(

lib/html_parser.dart

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:html/parser.dart' as parser;
66
import 'package:html/dom.dart' as dom;
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,6 +146,8 @@ class HtmlRichTextParser extends StatelessWidget {
144146
this.onLinkTap,
145147
this.renderNewlines = false,
146148
this.html,
149+
this.customTextStyle,
150+
this.customEdgeInsets,
147151
});
148152

149153
final double indentSize = 10.0;
@@ -152,6 +156,8 @@ class HtmlRichTextParser extends StatelessWidget {
152156
final onLinkTap;
153157
final bool renderNewlines;
154158
final String html;
159+
final CustomTextStyle customTextStyle;
160+
final CustomEdgeInsets customEdgeInsets;
155161

156162
// style elements set a default style
157163
// for all child nodes
@@ -472,6 +478,14 @@ class HtmlRichTextParser extends StatelessWidget {
472478
nextContext.blockType = 'blockquote';
473479
break;
474480
}
481+
482+
if (customTextStyle != null) {
483+
final TextStyle customStyle = customTextStyle(node, childStyle);
484+
if (customStyle != null) {
485+
childStyle = customStyle;
486+
}
487+
}
488+
475489
nextContext.childStyle = childStyle;
476490
}
477491

@@ -610,6 +624,11 @@ class HtmlRichTextParser extends StatelessWidget {
610624
parseContext.parentElement = null;
611625
TextAlign textAlign = TextAlign.left;
612626

627+
EdgeInsets _customEdgeInsets;
628+
if (customEdgeInsets != null) {
629+
_customEdgeInsets = customEdgeInsets(node);
630+
}
631+
613632
switch (node.localName) {
614633
case "hr":
615634
parseContext.rootWidgetList
@@ -715,10 +734,8 @@ class HtmlRichTextParser extends StatelessWidget {
715734
));
716735
}
717736
BlockText blockText = BlockText(
718-
margin: EdgeInsets.only(
719-
top: 8.0,
720-
bottom: 8.0,
721-
left: parseContext.indentLevel * indentSize),
737+
margin: _customEdgeInsets ??
738+
EdgeInsets.only(top: 8.0, bottom: 8.0, left: parseContext.indentLevel * indentSize),
722739
padding: EdgeInsets.all(2.0),
723740
decoration: decoration,
724741
child: RichText(

0 commit comments

Comments
 (0)