Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for borders, width, and height on tables #198

Merged
merged 1 commit into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
"li": Style(
// backgroundColor: Colors.red,
// fontSize: 20,
// fontSize: FontSize(20),
// margin: const EdgeInsets.only(top: 32),
),
"h1, h3, h5": Style(
Expand All @@ -128,8 +128,12 @@ class _MyHomePageState extends State<MyHomePage> {
"#whitespace": Style(
backgroundColor: Colors.deepPurple,
),
"table": Style(backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee)),
"tr": Style(border: Border(bottom: BorderSide(color: Colors.grey))),
"table": Style(
backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee),
),
"tr": Style(
border: Border(bottom: BorderSide(color: Colors.grey)),
),
"th": Style(
padding: EdgeInsets.all(6),
backgroundColor: Colors.grey,
Expand Down
113 changes: 62 additions & 51 deletions lib/src/layout_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,46 @@ class TableLayoutElement extends LayoutElement {

@override
Widget toWidget(RenderContext context) {

final colWidths = children.where((c) => c.name == "colgroup").map((group) {
return group.children.where((c) => c.name == "col").map((c) {
final widthStr = c.attributes["width"] ?? "";
if (widthStr.endsWith("%")) {
final width = double.tryParse(widthStr.substring(0, widthStr.length - 1)) * 0.01;
return FractionColumnWidth(width);
} else {
final width = double.tryParse(widthStr);
return FixedColumnWidth(width);
}
});
}).expand((i) => i).toList().asMap();
final colWidths = children
.where((c) => c.name == "colgroup")
.map((group) {
return group.children.where((c) => c.name == "col").map((c) {
final widthStr = c.attributes["width"] ?? "";
if (widthStr.endsWith("%")) {
final width = double.tryParse(widthStr.substring(0, widthStr.length - 1)) * 0.01;
return FractionColumnWidth(width);
} else {
final width = double.tryParse(widthStr);
return FixedColumnWidth(width);
}
});
})
.expand((i) => i)
.toList()
.asMap();

return Container(
decoration: BoxDecoration(
color: style.backgroundColor
color: style.backgroundColor,
border: style.border,
),
width: style.width,
height: style.height,
child: Table(
columnWidths: colWidths,
children: children.map((c) {
if (c is TableSectionLayoutElement) {
return c.toTableRows(context);
}
return null;
})
children: children
.map((c) {
if (c is TableSectionLayoutElement) {
return c.toTableRows(context);
}
return null;
})
.where((t) {
return t != null;
})
.toList()
.expand((i) => i)
.toList(),
return t != null;
})
.toList()
.expand((i) => i)
.toList(),
));
}
}
Expand Down Expand Up @@ -101,21 +109,26 @@ class TableRowLayoutElement extends LayoutElement {

TableRow toTableRow(RenderContext context) {
return TableRow(
decoration: BoxDecoration(
border: style.border,
),
children: children.map((c) {
if (c is StyledElement && c.name == 'td' || c.name == 'th') {
return TableCell(
child: Container(
padding: c.style.padding,
decoration: BoxDecoration(
color: c.style.backgroundColor
),
child: RichText(text: context.parser.parseTree(context, c))));
}
return null;
}).where((c) => c != null).toList());
decoration: BoxDecoration(
border: style.border,
color: style.backgroundColor,
),
children: children
.map((c) {
if (c is StyledElement && c.name == 'td' || c.name == 'th') {
return TableCell(
child: Container(
padding: c.style.padding,
decoration: BoxDecoration(
color: c.style.backgroundColor,
border: c.style.border,
),
child: RichText(text: context.parser.parseTree(context, c))));
}
return null;
})
.where((c) => c != null)
.toList());
}
}

Expand All @@ -128,22 +141,24 @@ class TableStyleElement extends StyledElement {
}) : super(name: name, children: children, style: style, node: node);
}

TableStyleElement parseTableDefinitionElement(
dom.Element element, List<StyledElement> children) {
TableStyleElement parseTableDefinitionElement(dom.Element element, List<StyledElement> children) {
switch (element.localName) {
case "colgroup":
case "col":
return TableStyleElement(
name: element.localName,
children: children,
node: element
name: element.localName,
children: children,
node: element,
);
default:
return TableStyleElement();
}
}

LayoutElement parseLayoutElement(
dom.Element element, List<StyledElement> children) {
dom.Element element,
List<StyledElement> children,
) {
switch (element.localName) {
case "table":
return TableLayoutElement(
Expand All @@ -161,11 +176,7 @@ LayoutElement parseLayoutElement(
);
break;
case "tr":
return TableRowLayoutElement(
name: element.localName,
children: children,
node: element
);
return TableRowLayoutElement(name: element.localName, children: children, node: element);
break;
default:
return TableLayoutElement(children: children);
Expand Down