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

Table widget refactors #1776

Merged
merged 2 commits into from
Nov 26, 2024
Merged
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
67 changes: 35 additions & 32 deletions pdf/lib/src/widgets/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ class TableBorder extends Border {
}) {
final side = BorderSide(color: color, width: width, style: style);
return TableBorder(
top: side,
right: side,
bottom: side,
left: side,
horizontalInside: side,
verticalInside: side);
top: side,
right: side,
bottom: side,
left: side,
horizontalInside: side,
verticalInside: side,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added trailing comma to force formatting

);
}

/// Creates a border for a table where all the interior sides use the same styling and all the exterior sides use the same styling.
Expand Down Expand Up @@ -149,8 +150,8 @@ class TableContext extends WidgetContext {
class ColumnLayout {
ColumnLayout(this.width, this.flex);

final double? width;
final double? flex;
final double width;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the null here makes the amount the work later on easier. I couldn't find a case where they would be.

final double flex;
}

abstract class TableColumnWidth {
Expand All @@ -167,9 +168,12 @@ class IntrinsicColumnWidth extends TableColumnWidth {

@override
ColumnLayout layout(
Widget child, Context context, BoxConstraints constraints) {
Widget child,
Context context,
BoxConstraints constraints,
) {
if (flex != null) {
return ColumnLayout(0, flex);
return ColumnLayout(0, flex!);
}

child.layout(context, const BoxConstraints());
Expand Down Expand Up @@ -315,7 +319,7 @@ class Table extends Widget with SpanningWidget {

final TableWidth tableWidth;

final List<double?> _widths = <double?>[];
final List<double> _widths = <double>[];
final List<double> _heights = <double>[];

final TableContext _context = TableContext();
Expand All @@ -338,28 +342,27 @@ class Table extends Widget with SpanningWidget {
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
// Compute required width for all row/columns width flex
final flex = <double?>[];
final flex = <double>[];
_widths.clear();
_heights.clear();
var index = 0;

for (final row in children) {
var n = 0;
for (final child in row.children) {
final columnWidth = columnWidths != null && columnWidths![n] != null
? columnWidths![n]!
: defaultColumnWidth;
for (final entry in row.children.asMap().entries) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this is a bit more legible and no longer has to worry about null as much

final index = entry.key;
final child = entry.value;
final columnWidth = columnWidths?[index] ?? defaultColumnWidth;
final columnLayout = columnWidth.layout(child, context, constraints);
if (flex.length < n + 1) {

if (index >= flex.length) {
flex.add(columnLayout.flex);
_widths.add(columnLayout.width);
} else {
if (columnLayout.flex! > 0) {
flex[n] = math.max(flex[n]!, columnLayout.flex!);
if (columnLayout.flex > 0) {
flex[index] = math.max(flex[index], columnLayout.flex);
}
_widths[n] = math.max(_widths[n]!, columnLayout.width!);
_widths[index] = math.max(_widths[index], columnLayout.width);
}
n++;
}
}

Expand All @@ -368,35 +371,35 @@ class Table extends Widget with SpanningWidget {
return;
}

final maxWidth = _widths.reduce((double? a, double? b) => a! + b!);
final maxWidth = _widths.fold(0.0, (sum, element) => sum + element);

// Compute column widths using flex and estimated width
if (constraints.hasBoundedWidth) {
final totalFlex = flex.reduce((double? a, double? b) => a! + b!)!;
final totalFlex = flex.reduce((double? a, double? b) => a! + b!);
var flexSpace = 0.0;
for (var n = 0; n < _widths.length; n++) {
if (flex[n] == 0.0) {
final newWidth = _widths[n]! / maxWidth! * constraints.maxWidth;
final newWidth = _widths[n] / maxWidth * constraints.maxWidth;
if ((tableWidth == TableWidth.max && totalFlex == 0.0) ||
newWidth < _widths[n]!) {
newWidth < _widths[n]) {
_widths[n] = newWidth;
}
flexSpace += _widths[n]!;
flexSpace += _widths[n];
}
}
final spacePerFlex = totalFlex > 0.0
? ((constraints.maxWidth - flexSpace) / totalFlex)
: double.nan;

for (var n = 0; n < _widths.length; n++) {
if (flex[n]! > 0.0) {
final newWidth = spacePerFlex * flex[n]!;
if (flex[n] > 0.0) {
final newWidth = spacePerFlex * flex[n];
_widths[n] = newWidth;
}
}
}

final totalWidth = _widths.reduce((double? a, double? b) => a! + b!)!;
final totalWidth = _widths.fold(0.0, (sum, element) => sum + element);

// Compute final widths
var totalHeight = 0.0;
Expand All @@ -416,7 +419,7 @@ class Table extends Widget with SpanningWidget {
assert(child.box != null);
child.box =
PdfRect(x, totalHeight, child.box!.width, child.box!.height);
x += _widths[n]!;
x += _widths[n];
lineHeight = math.max(lineHeight, child.box!.height);
n++;
}
Expand All @@ -434,7 +437,7 @@ class Table extends Widget with SpanningWidget {
assert(child.box != null);
child.box =
PdfRect(x, totalHeight, child.box!.width, child.box!.height);
x += _widths[n]!;
x += _widths[n];
n++;
}
}
Expand Down
Loading