From b23d9173d27ab011da2a2e7940ae69d36f421e0c Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Sun, 8 Sep 2024 14:34:38 +0200 Subject: [PATCH] fill spanfilledchildren --- pdf/lib/src/widgets/table.dart | 91 +++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/pdf/lib/src/widgets/table.dart b/pdf/lib/src/widgets/table.dart index 184930d1..3707306a 100644 --- a/pdf/lib/src/widgets/table.dart +++ b/pdf/lib/src/widgets/table.dart @@ -25,17 +25,52 @@ import '../../widgets.dart'; /// A horizontal group of cells in a [Table]. @immutable class TableRow { - const TableRow({ + TableRow({ required this.children, this.repeat = false, this.verticalAlignment, this.decoration, this.columnSpans, - }); + }) { + if (columnSpans == null) { + spanFilledChildren = children; + return; + } + var n = 0; + // TODO: handle intrinsic layout + spanFilledChildren = []; + for (final child in children) { + // Columns, which are currently spanned. + final columnSpan = columnSpans![n] ?? 1; + spanFilledChildren.add(child); + if (columnSpan > 1) { + spanFilledChildren + .addAll(Iterable.generate(columnSpan - 1, (index) => Container())); + } + n += columnSpan; + } + // final indices = Iterable.generate(columnSpan, (span) => n + span); + // + // final columnLayout = + // indices.fold(ColumnLayout(null, null), (prev, curIndex) { + // final curColumnWidth = _getTableColumnWidth(curIndex); + // final currentColumnLayout = + // curColumnWidth.layout(child, context, constraints); + // return ColumnLayout( + // (prev.width == null && currentColumnLayout.width == null) + // ? null + // : (prev.width ?? 0) + (currentColumnLayout.width ?? 0), + // (prev.flex == null && currentColumnLayout.flex == null) + // ? null + // : (prev.flex ?? 0) + (currentColumnLayout.flex ?? 0)); + // }); + } /// The widgets that comprise the cells in this row. final List children; + late final List spanFilledChildren; + /// Repeat this row on all pages final bool repeat; @@ -337,48 +372,22 @@ class Table extends Widget with SpanningWidget { _context.firstLine = _context.lastLine; } - TableColumnWidth _getTableColumnWidth(int n) { - final colWidths = columnWidths; - if (colWidths == null) { - return defaultColumnWidth; - } - final curColWidth = colWidths[n]; - if (curColWidth == null) { - return defaultColumnWidth; - } - return curColWidth; - } - @override void layout(Context context, BoxConstraints constraints, {bool parentUsesSize = false}) { // Compute required width for all row/columns width flex final flex = []; + final widths = []; _heights.clear(); var index = 0; for (final row in children) { - final widths = []; var n = 0; - for (final child in row.children) { - final columnSpans = row.columnSpans; - // Columns, which are currently spanned. - final columnSpan = columnSpans?[n] ?? 1; - final indices = Iterable.generate(columnSpan, (span) => n + span); - - final columnLayout = - indices.fold(ColumnLayout(null, null), (prev, curIndex) { - final curColumnWidth = _getTableColumnWidth(curIndex); - final currentColumnLayout = - curColumnWidth.layout(child, context, constraints); - return ColumnLayout( - (prev.width == null && currentColumnLayout.width == null) - ? null - : (prev.width ?? 0) + (currentColumnLayout.width ?? 0), - (prev.flex == null && currentColumnLayout.flex == null) - ? null - : (prev.flex ?? 0) + (currentColumnLayout.flex ?? 0)); - }); + for (final child in row.spanFilledChildren) { + final columnWidth = columnWidths != null && columnWidths![n] != null + ? columnWidths![n]! + : defaultColumnWidth; + final columnLayout = columnWidth.layout(child, context, constraints); if (flex.length < n + 1) { flex.add(columnLayout.flex); widths.add(columnLayout.width); @@ -388,7 +397,7 @@ class Table extends Widget with SpanningWidget { } widths[n] = math.max(widths[n]!, columnLayout.width!); } - n += columnSpan ?? 1; + n++; } } @@ -439,7 +448,7 @@ class Table extends Widget with SpanningWidget { var x = 0.0; var lineHeight = 0.0; - for (final child in row.children) { + for (final child in row.spanFilledChildren) { final childConstraints = BoxConstraints.tightFor(width: widths[n]); child.layout(context, childConstraints); assert(child.box != null); @@ -456,7 +465,7 @@ class Table extends Widget with SpanningWidget { // Compute the layout again to give the full height to all cells n = 0; x = 0; - for (final child in row.children) { + for (final child in row.spanFilledChildren) { final childConstraints = BoxConstraints.tightFor(width: widths[n], height: lineHeight); child.layout(context, childConstraints); @@ -487,7 +496,7 @@ class Table extends Widget with SpanningWidget { final align = row.verticalAlignment ?? defaultVerticalAlignment; - for (final child in row.children) { + for (final child in row.spanFilledChildren) { double? childY; switch (align) { @@ -548,7 +557,7 @@ class Table extends Widget with SpanningWidget { if (row.decoration != null) { var y = double.infinity; var h = 0.0; - for (final child in row.children) { + for (final child in row.spanFilledChildren) { y = math.min(y, child.box!.y); h = math.max(h, child.box!.height); } @@ -559,7 +568,7 @@ class Table extends Widget with SpanningWidget { ); } - for (final child in row.children) { + for (final child in row.spanFilledChildren) { context.canvas ..saveContext() ..drawRect( @@ -582,7 +591,7 @@ class Table extends Widget with SpanningWidget { if (row.decoration != null) { var y = double.infinity; var h = 0.0; - for (final child in row.children) { + for (final child in row.spanFilledChildren) { y = math.min(y, child.box!.y); h = math.max(h, child.box!.height); }