From 1a6272eb16fdc8d92aeab57208e361350bf6165f Mon Sep 17 00:00:00 2001 From: Graham Smith Date: Tue, 12 Nov 2024 21:37:46 +0000 Subject: [PATCH 1/2] Table widget refactors --- pdf/lib/src/widgets/table.dart | 65 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/pdf/lib/src/widgets/table.dart b/pdf/lib/src/widgets/table.dart index 38df5d9f..179c4691 100644 --- a/pdf/lib/src/widgets/table.dart +++ b/pdf/lib/src/widgets/table.dart @@ -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, + ); } /// Creates a border for a table where all the interior sides use the same styling and all the exterior sides use the same styling. @@ -149,8 +150,8 @@ class TableContext extends WidgetContext { class ColumnLayout { ColumnLayout(this.width, this.flex); - final double? width; - final double? flex; + final double width; + final double flex; } abstract class TableColumnWidth { @@ -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()); @@ -315,7 +319,7 @@ class Table extends Widget with SpanningWidget { final TableWidth tableWidth; - final List _widths = []; + final List _widths = []; final List _heights = []; final TableContext _context = TableContext(); @@ -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 = []; + final flex = []; _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) { + 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++; } } @@ -372,16 +375,16 @@ class Table extends Widget with SpanningWidget { // 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 @@ -389,14 +392,14 @@ class Table extends Widget with SpanningWidget { : 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.reduce((double? a, double? b) => a! + b!); // Compute final widths var totalHeight = 0.0; @@ -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++; } @@ -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++; } } From ee9d075f23340ab9e6ea55075252269e7af6ac6c Mon Sep 17 00:00:00 2001 From: Graham Smith Date: Wed, 13 Nov 2024 10:58:10 +0000 Subject: [PATCH 2/2] Table widget refactor totalWidth to remove null checking and empty --- pdf/lib/src/widgets/table.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pdf/lib/src/widgets/table.dart b/pdf/lib/src/widgets/table.dart index 179c4691..c8ad1314 100644 --- a/pdf/lib/src/widgets/table.dart +++ b/pdf/lib/src/widgets/table.dart @@ -371,7 +371,7 @@ 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) { @@ -399,7 +399,7 @@ class Table extends Widget with SpanningWidget { } } - 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;