Skip to content

Commit b23d917

Browse files
committed
fill spanfilledchildren
1 parent eab6461 commit b23d917

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

pdf/lib/src/widgets/table.dart

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,52 @@ import '../../widgets.dart';
2525
/// A horizontal group of cells in a [Table].
2626
@immutable
2727
class TableRow {
28-
const TableRow({
28+
TableRow({
2929
required this.children,
3030
this.repeat = false,
3131
this.verticalAlignment,
3232
this.decoration,
3333
this.columnSpans,
34-
});
34+
}) {
35+
if (columnSpans == null) {
36+
spanFilledChildren = children;
37+
return;
38+
}
39+
var n = 0;
40+
// TODO: handle intrinsic layout
41+
spanFilledChildren = <Widget>[];
42+
for (final child in children) {
43+
// Columns, which are currently spanned.
44+
final columnSpan = columnSpans![n] ?? 1;
45+
spanFilledChildren.add(child);
46+
if (columnSpan > 1) {
47+
spanFilledChildren
48+
.addAll(Iterable.generate(columnSpan - 1, (index) => Container()));
49+
}
50+
n += columnSpan;
51+
}
52+
// final indices = Iterable.generate(columnSpan, (span) => n + span);
53+
//
54+
// final columnLayout =
55+
// indices.fold(ColumnLayout(null, null), (prev, curIndex) {
56+
// final curColumnWidth = _getTableColumnWidth(curIndex);
57+
// final currentColumnLayout =
58+
// curColumnWidth.layout(child, context, constraints);
59+
// return ColumnLayout(
60+
// (prev.width == null && currentColumnLayout.width == null)
61+
// ? null
62+
// : (prev.width ?? 0) + (currentColumnLayout.width ?? 0),
63+
// (prev.flex == null && currentColumnLayout.flex == null)
64+
// ? null
65+
// : (prev.flex ?? 0) + (currentColumnLayout.flex ?? 0));
66+
// });
67+
}
3568

3669
/// The widgets that comprise the cells in this row.
3770
final List<Widget> children;
3871

72+
late final List<Widget> spanFilledChildren;
73+
3974
/// Repeat this row on all pages
4075
final bool repeat;
4176

@@ -337,48 +372,22 @@ class Table extends Widget with SpanningWidget {
337372
_context.firstLine = _context.lastLine;
338373
}
339374

340-
TableColumnWidth _getTableColumnWidth(int n) {
341-
final colWidths = columnWidths;
342-
if (colWidths == null) {
343-
return defaultColumnWidth;
344-
}
345-
final curColWidth = colWidths[n];
346-
if (curColWidth == null) {
347-
return defaultColumnWidth;
348-
}
349-
return curColWidth;
350-
}
351-
352375
@override
353376
void layout(Context context, BoxConstraints constraints,
354377
{bool parentUsesSize = false}) {
355378
// Compute required width for all row/columns width flex
356379
final flex = <double?>[];
380+
final widths = <double?>[];
357381
_heights.clear();
358382
var index = 0;
359383

360384
for (final row in children) {
361-
final widths = <double?>[];
362385
var n = 0;
363-
for (final child in row.children) {
364-
final columnSpans = row.columnSpans;
365-
// Columns, which are currently spanned.
366-
final columnSpan = columnSpans?[n] ?? 1;
367-
final indices = Iterable.generate(columnSpan, (span) => n + span);
368-
369-
final columnLayout =
370-
indices.fold(ColumnLayout(null, null), (prev, curIndex) {
371-
final curColumnWidth = _getTableColumnWidth(curIndex);
372-
final currentColumnLayout =
373-
curColumnWidth.layout(child, context, constraints);
374-
return ColumnLayout(
375-
(prev.width == null && currentColumnLayout.width == null)
376-
? null
377-
: (prev.width ?? 0) + (currentColumnLayout.width ?? 0),
378-
(prev.flex == null && currentColumnLayout.flex == null)
379-
? null
380-
: (prev.flex ?? 0) + (currentColumnLayout.flex ?? 0));
381-
});
386+
for (final child in row.spanFilledChildren) {
387+
final columnWidth = columnWidths != null && columnWidths![n] != null
388+
? columnWidths![n]!
389+
: defaultColumnWidth;
390+
final columnLayout = columnWidth.layout(child, context, constraints);
382391
if (flex.length < n + 1) {
383392
flex.add(columnLayout.flex);
384393
widths.add(columnLayout.width);
@@ -388,7 +397,7 @@ class Table extends Widget with SpanningWidget {
388397
}
389398
widths[n] = math.max(widths[n]!, columnLayout.width!);
390399
}
391-
n += columnSpan ?? 1;
400+
n++;
392401
}
393402
}
394403

@@ -439,7 +448,7 @@ class Table extends Widget with SpanningWidget {
439448
var x = 0.0;
440449

441450
var lineHeight = 0.0;
442-
for (final child in row.children) {
451+
for (final child in row.spanFilledChildren) {
443452
final childConstraints = BoxConstraints.tightFor(width: widths[n]);
444453
child.layout(context, childConstraints);
445454
assert(child.box != null);
@@ -456,7 +465,7 @@ class Table extends Widget with SpanningWidget {
456465
// Compute the layout again to give the full height to all cells
457466
n = 0;
458467
x = 0;
459-
for (final child in row.children) {
468+
for (final child in row.spanFilledChildren) {
460469
final childConstraints =
461470
BoxConstraints.tightFor(width: widths[n], height: lineHeight);
462471
child.layout(context, childConstraints);
@@ -487,7 +496,7 @@ class Table extends Widget with SpanningWidget {
487496

488497
final align = row.verticalAlignment ?? defaultVerticalAlignment;
489498

490-
for (final child in row.children) {
499+
for (final child in row.spanFilledChildren) {
491500
double? childY;
492501

493502
switch (align) {
@@ -548,7 +557,7 @@ class Table extends Widget with SpanningWidget {
548557
if (row.decoration != null) {
549558
var y = double.infinity;
550559
var h = 0.0;
551-
for (final child in row.children) {
560+
for (final child in row.spanFilledChildren) {
552561
y = math.min(y, child.box!.y);
553562
h = math.max(h, child.box!.height);
554563
}
@@ -559,7 +568,7 @@ class Table extends Widget with SpanningWidget {
559568
);
560569
}
561570

562-
for (final child in row.children) {
571+
for (final child in row.spanFilledChildren) {
563572
context.canvas
564573
..saveContext()
565574
..drawRect(
@@ -582,7 +591,7 @@ class Table extends Widget with SpanningWidget {
582591
if (row.decoration != null) {
583592
var y = double.infinity;
584593
var h = 0.0;
585-
for (final child in row.children) {
594+
for (final child in row.spanFilledChildren) {
586595
y = math.min(y, child.box!.y);
587596
h = math.max(h, child.box!.height);
588597
}

0 commit comments

Comments
 (0)