Skip to content

Commit 98b713e

Browse files
committed
fixed ordered list numbering
1 parent 8fcdd43 commit 98b713e

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

lib/flutter_html.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Html extends StatelessWidget {
99
@required this.data,
1010
this.padding,
1111
this.backgroundColor,
12-
this.defaultTextStyle = const TextStyle(color: Colors.black),
12+
this.defaultTextStyle,
1313
this.onLinkTap,
1414
this.renderNewlines = false,
1515
this.customRender,
@@ -37,22 +37,20 @@ class Html extends StatelessWidget {
3737
color: backgroundColor,
3838
width: width,
3939
child: DefaultTextStyle.merge(
40-
style: defaultTextStyle,
41-
child: Wrap(
42-
alignment: WrapAlignment.start,
43-
children: (useRichText)
44-
? HtmlRichTextParser(
45-
width: width,
46-
onLinkTap: onLinkTap,
47-
renderNewlines: renderNewlines,
48-
customRender: customRender,
49-
).parse(data)
50-
: HtmlOldParser(
51-
width: width,
52-
onLinkTap: onLinkTap,
53-
renderNewlines: renderNewlines,
54-
customRender: customRender,
55-
).parse(data),
40+
style: defaultTextStyle ?? DefaultTextStyle.of(context).style,
41+
child: (useRichText)
42+
? HtmlRichTextParser(
43+
width: width,
44+
onLinkTap: onLinkTap,
45+
renderNewlines: renderNewlines,
46+
html: data,
47+
)
48+
: HtmlOldParser(
49+
width: width,
50+
onLinkTap: onLinkTap,
51+
renderNewlines: renderNewlines,
52+
customRender: customRender,
53+
html: data,
5654
),
5755
),
5856
);

lib/html_parser.dart

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,20 @@ class ParseContext {
117117
}
118118
}
119119

120-
class HtmlRichTextParser {
120+
class HtmlRichTextParser extends StatelessWidget{
121121
HtmlRichTextParser({
122122
@required this.width,
123123
this.onLinkTap,
124124
this.renderNewlines = false,
125-
this.customRender,
126-
this.context,
125+
this.html,
127126
});
128127

129128
final double indentSize = 10.0;
130129

131130
final double width;
132131
final onLinkTap;
133132
final bool renderNewlines;
134-
final CustomRender customRender;
135-
final BuildContext context;
133+
final String html;
136134

137135
// style elements set a default style
138136
// for all child nodes
@@ -210,7 +208,10 @@ class HtmlRichTextParser {
210208
}
211209

212210
// Parses an html string and returns a list of RichText widgets that represent the body of your html document.
213-
List<Widget> parse(String data) {
211+
212+
@override
213+
Widget build(BuildContext context) {
214+
String data = html;
214215

215216
if (renderNewlines) {
216217
data = data.replaceAll("\n", "<br />");
@@ -229,7 +230,7 @@ class HtmlRichTextParser {
229230
// _parseNode(body, parseContext);
230231

231232
// eliminate empty widgets
232-
List<Widget> retval = [];
233+
List<Widget> children = [];
233234
widgetList.forEach((dynamic w){
234235
if (w is BlockText) {
235236
if (w.child.text == null) return;
@@ -239,9 +240,12 @@ class HtmlRichTextParser {
239240
} else if (w is LinkTextSpan) {
240241
if (w.text.isEmpty && w.children.isEmpty) return;
241242
}
242-
retval.add(w);
243+
children.add(w);
243244
});
244-
return retval;
245+
246+
return Column(
247+
children: children,
248+
);
245249
}
246250

247251
// THE WORKHORSE FUNCTION!!
@@ -492,8 +496,10 @@ class HtmlRichTextParser {
492496
case "li":
493497
String leadingChar = parseContext.listChar;
494498
if (parseContext.blockType == 'ol') {
495-
nextContext.listCount += 1;
496-
leadingChar = nextContext.listCount.toString() + '.';
499+
// nextContext will handle nodes under this 'li'
500+
// but we want to increment the count at this level
501+
parseContext.listCount += 1;
502+
leadingChar = parseContext.listCount.toString() + '.';
497503
}
498504
BlockText blockText = BlockText(
499505
margin:EdgeInsets.only(left: parseContext.indentLevel * indentSize, top:3.0),
@@ -651,18 +657,20 @@ class HtmlRichTextParser {
651657

652658

653659

654-
class HtmlOldParser {
660+
class HtmlOldParser extends StatelessWidget {
655661
HtmlOldParser({
656662
@required this.width,
657663
this.onLinkTap,
658664
this.renderNewlines = false,
659665
this.customRender,
666+
this.html,
660667
});
661668

662669
final double width;
663670
final OnLinkTap onLinkTap;
664671
final bool renderNewlines;
665672
final CustomRender customRender;
673+
final String html;
666674

667675
static const _supportedElements = [
668676
"a",
@@ -739,6 +747,14 @@ class HtmlOldParser {
739747
"var",
740748
];
741749

750+
@override
751+
Widget build(BuildContext context) {
752+
return Wrap(
753+
alignment: WrapAlignment.start,
754+
children: parse(html),
755+
);
756+
}
757+
742758
///Parses an html string and returns a list of widgets that represent the body of your html document.
743759
List<Widget> parse(String data) {
744760
List<Widget> widgetList = new List<Widget>();

0 commit comments

Comments
 (0)