Skip to content

Commit

Permalink
fill spanfilledchildren
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Sep 8, 2024
1 parent eab6461 commit b23d917
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions pdf/lib/src/widgets/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Widget>[];
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<Widget> children;

late final List<Widget> spanFilledChildren;

/// Repeat this row on all pages
final bool repeat;

Expand Down Expand Up @@ -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 = <double?>[];
final widths = <double?>[];
_heights.clear();
var index = 0;

for (final row in children) {
final widths = <double?>[];
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);
Expand All @@ -388,7 +397,7 @@ class Table extends Widget with SpanningWidget {
}
widths[n] = math.max(widths[n]!, columnLayout.width!);
}
n += columnSpan ?? 1;
n++;
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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(
Expand All @@ -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);
}
Expand Down

0 comments on commit b23d917

Please sign in to comment.