Skip to content

Commit b57450b

Browse files
committed
finished basic table rendering
1 parent a4d32db commit b57450b

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

lib/html_parser.dart

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class HtmlRichTextParser extends StatelessWidget {
175175
"br",
176176
"table",
177177
"tbody",
178+
"caption",
178179
"td",
179180
"tfoot",
180181
"th",
@@ -401,7 +402,7 @@ class HtmlRichTextParser extends StatelessWidget {
401402
// OTHER ELEMENT NODES
402403
else if (node is dom.Element) {
403404
assert(() {
404-
// debugPrint("Found ${node.localName}");
405+
debugPrint("Found ${node.localName}");
405406
// debugPrint(node.outerHtml);
406407
return true;
407408
}());
@@ -532,26 +533,15 @@ class HtmlRichTextParser extends StatelessWidget {
532533
crossAxisAlignment: CrossAxisAlignment.start,
533534
children: <Widget>[],
534535
);
535-
nextContext.rootWidgetList.add(nextContext.parentElement);
536+
nextContext.rootWidgetList.add(Container(
537+
margin: EdgeInsets.symmetric(vertical: 12.0),
538+
child: nextContext.parentElement));
536539
break;
537540

538-
// we don't handle tbody or thead elements separately for now
541+
// we don't handle tbody, thead, or tfoot elements separately for now
539542
case "tbody":
540543
case "thead":
541-
break;
542-
543-
// caption elements throw us off
544-
case "caption":
545-
RichText text =
546-
RichText(text: TextSpan(text: '', children: <TextSpan>[]));
547-
Row row = Row(
548-
crossAxisAlignment: CrossAxisAlignment.center,
549-
children: <Widget>[
550-
text,
551-
],
552-
);
553-
nextContext.parentElement.children.add(row);
554-
nextContext.parentElement = text.text;
544+
case "tfoot":
555545
break;
556546

557547
case "td":
@@ -560,11 +550,15 @@ class HtmlRichTextParser extends StatelessWidget {
560550
if (node.attributes['colspan'] != null) {
561551
colspan = int.tryParse(node.attributes['colspan']);
562552
}
553+
nextContext.childStyle = nextContext.childStyle.merge(TextStyle(
554+
fontWeight: (node.localName == 'th')
555+
? FontWeight.bold
556+
: FontWeight.normal));
563557
RichText text =
564558
RichText(text: TextSpan(text: '', children: <TextSpan>[]));
565559
Expanded cell = Expanded(
566560
flex: colspan,
567-
child: text,
561+
child: Container(padding: EdgeInsets.all(1.0), child: text),
568562
);
569563
nextContext.parentElement.children.add(cell);
570564
nextContext.parentElement = text.text;
@@ -578,6 +572,27 @@ class HtmlRichTextParser extends StatelessWidget {
578572
nextContext.parentElement.children.add(row);
579573
nextContext.parentElement = row;
580574
break;
575+
576+
// treat captions like a row with one expanded cell
577+
case "caption":
578+
// create the row
579+
Row row = Row(
580+
crossAxisAlignment: CrossAxisAlignment.center,
581+
children: <Widget>[],
582+
);
583+
584+
// create an expanded cell
585+
RichText text = RichText(
586+
textAlign: TextAlign.center,
587+
textScaleFactor: 1.2,
588+
text: TextSpan(text: '', children: <TextSpan>[]));
589+
Expanded cell = Expanded(
590+
child: Container(padding: EdgeInsets.all(2.0), child: text),
591+
);
592+
row.children.add(cell);
593+
nextContext.parentElement.children.add(row);
594+
nextContext.parentElement = text.text;
595+
break;
581596
}
582597
}
583598

0 commit comments

Comments
 (0)