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

[WIP] Table merge cells #1669

Closed
wants to merge 1 commit into from
Closed
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
95 changes: 84 additions & 11 deletions pdf/lib/src/widgets/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,23 @@ class Table extends Widget with SpanningWidget {
var x = 0.0;

var lineHeight = 0.0;
var colSpan = 0;
for (final child in row.children) {
final childConstraints = BoxConstraints.tightFor(width: _widths[n]);
// Adjust width based on colSpan
var width = _getWidth(child, n);
if (child is TableCell && child.colSpan != null) {
colSpan = child.colSpan! - 1;
} else if (colSpan > 0) {
width = 0;
colSpan--;
}

final childConstraints = BoxConstraints.tightFor(width: width);
child.layout(context, childConstraints);
assert(child.box != null);
child.box =
PdfRect(x, totalHeight, child.box!.width, child.box!.height);
x += _widths[n]!;
x += width;
lineHeight = math.max(lineHeight, child.box!.height);
n++;
}
Expand All @@ -427,14 +437,24 @@ class Table extends Widget with SpanningWidget {
// Compute the layout again to give the full height to all cells
n = 0;
x = 0;
var colSpan = 0;
for (final child in row.children) {
// Adjust width based on colSpan
var width = _getWidth(child, n);
if (child is TableCell && child.colSpan != null) {
colSpan = child.colSpan! - 1;
} else if (colSpan > 0) {
width = 0;
colSpan--;
}

final childConstraints =
BoxConstraints.tightFor(width: _widths[n], height: lineHeight);
BoxConstraints.tightFor(width: width, height: lineHeight);
child.layout(context, childConstraints);
assert(child.box != null);
child.box =
PdfRect(x, totalHeight, child.box!.width, child.box!.height);
x += _widths[n]!;
x += width;
n++;
}
}
Expand All @@ -451,24 +471,34 @@ class Table extends Widget with SpanningWidget {
// Compute final y position
index = 0;
var heightIndex = 0;
final spanIndex = {};
for (final row in children) {
if (index++ < _context.firstLine && !row.repeat) {
continue;
}

final align = row.verticalAlignment ?? defaultVerticalAlignment;

var n = 0;
for (final child in row.children) {
double? childY;

// Adjust height based on rowSpan
var height = _getHeight(child, heightIndex);
if (child is TableCell && child.rowSpan != null) {
spanIndex[n] = child.rowSpan! - 1;
} else if (spanIndex[n] != null && spanIndex[n] > 0) {
height = 0;
spanIndex[n] -= 1;
}

switch (align) {
case TableCellVerticalAlignment.bottom:
childY = totalHeight - child.box!.y - _getHeight(heightIndex);
childY = totalHeight - child.box!.y - height;
break;
case TableCellVerticalAlignment.middle:
childY = totalHeight -
child.box!.y -
(_getHeight(heightIndex) + child.box!.height) / 2;
childY =
totalHeight - child.box!.y - (height + child.box!.height) / 2;
break;
case TableCellVerticalAlignment.top:
case TableCellVerticalAlignment.full:
Expand All @@ -480,8 +510,9 @@ class Table extends Widget with SpanningWidget {
child.box!.x,
childY,
child.box!.width,
child.box!.height,
height,
);
n++;
}

if (index >= _context.lastLine) {
Expand Down Expand Up @@ -573,9 +604,51 @@ class Table extends Widget with SpanningWidget {
}
}

double _getHeight(int heightIndex) {
return (heightIndex >= 0 && heightIndex < _heights.length)
double _getHeight(Widget cell, int heightIndex) {
double? height = (heightIndex >= 0 && heightIndex < _heights.length)
? _heights[heightIndex]
: 0.0;
if (cell is TableCell && cell.rowSpan != null) {
double? newHeight = 0;
for (var i = 0; i < cell.rowSpan!; i++) {
newHeight = newHeight! + _heights[heightIndex + i];
}
height = newHeight;
}
return height ?? 0;
}

double _getWidth(Widget cell, int widthIndex) {
var width = (widthIndex >= 0 && widthIndex < _widths.length)
? _widths[widthIndex]
: 0.0;
if (cell is TableCell && cell.colSpan != null) {
double? newWidth = 0;
for (var i = 0; i < cell.colSpan!; i++) {
newWidth = newWidth! + _widths[widthIndex + i]!;
}
width = newWidth;
}
return width ?? 0;
}
}

class TableCell extends Container {
TableCell({
super.alignment,
super.padding,
super.color,
super.decoration,
super.foregroundDecoration,
super.width,
super.height,
super.constraints,
super.margin,
super.transform,
super.child,
this.colSpan,
this.rowSpan,
});
int? colSpan;
int? rowSpan;
}