Skip to content
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
3 changes: 1 addition & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ const htmlData = """
<table>
<colgroup>
<col width="50%" />
<col width="25%" />
<col width="25%" />
<col span="2" width="25%" />
</colgroup>
<thead>
<tr><th>One</th><th>Two</th><th>Three</th></tr>
Expand Down
53 changes: 33 additions & 20 deletions lib/src/layout_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,35 @@ class TableLayoutElement extends LayoutElement {
@override
Widget toWidget(RenderContext context) {
final rows = <TableRowLayoutElement>[];
List<TrackSize> columnSizes;
List<TrackSize> columnSizes = <TrackSize>[];
for (var child in children) {
if (child is TableStyleElement) {
// Map <col> tags to predetermined column track sizes
columnSizes = child.children.where((c) => c.name == "col").map((c) {
final colWidth = c.attributes["width"];
if (colWidth != null && colWidth.endsWith("%")) {
final percentageSize =
double.tryParse(colWidth.substring(0, colWidth.length - 1));
return percentageSize != null
? FlexibleTrackSize(percentageSize * 0.01)
: FlexibleTrackSize(1);
} else if (colWidth != null) {
final fixedPxSize = double.tryParse(colWidth);
return fixedPxSize != null
? FixedTrackSize(fixedPxSize)
: FlexibleTrackSize(1);
} else {
return FlexibleTrackSize(1);
}
}).toList(growable: false);
columnSizes = child.children
.where((c) => c.name == "col")
.map((c) {
final span =
int.parse(c.attributes["span"] ?? "1", onError: (_) => 1);
final colWidth = c.attributes["width"];
return List.generate(span, (index) {
if (colWidth != null && colWidth.endsWith("%")) {
final percentageSize = double.tryParse(
colWidth.substring(0, colWidth.length - 1));
return percentageSize != null
? FlexibleTrackSize(percentageSize * 0.01)
: FlexibleTrackSize(1);
} else if (colWidth != null) {
final fixedPxSize = double.tryParse(colWidth);
return fixedPxSize != null
? FixedTrackSize(fixedPxSize)
: FlexibleTrackSize(1);
} else {
return FlexibleTrackSize(1);
}
});
})
.expand((element) => element)
.toList(growable: false);
} else if (child is TableSectionLayoutElement) {
rows.addAll(child.children.whereType());
} else if (child is TableRowLayoutElement) {
Expand All @@ -71,6 +79,7 @@ class TableLayoutElement extends LayoutElement {
.fold(0, (int value, child) => value + child.colspan))
.fold(0, max);

// Place the cells in the rows/columns
final cells = <GridPlacement>[];
final columnRowOffset = List.generate(columnMax + 1, (_) => 0);
int rowi = 0;
Expand Down Expand Up @@ -113,8 +122,12 @@ class TableLayoutElement extends LayoutElement {
rowi++;
}

final finalColumnSizes =
columnSizes ?? List.generate(columnMax, (_) => FlexibleTrackSize(1));
// Create column tracks (insofar there were no colgroups that already defined them)
List<TrackSize> finalColumnSizes = (columnSizes ?? <TrackSize>[]).take(
columnMax).toList();
finalColumnSizes += List.generate(
max(0, columnMax - finalColumnSizes.length),
(_) => FlexibleTrackSize(1));
return Container(
decoration: BoxDecoration(
color: style.backgroundColor,
Expand Down