Committed by
GitHub
Table widget refactors (#1776)
* Table widget refactors * Table widget refactor totalWidth to remove null checking and empty
Showing
1 changed file
with
30 additions
and
27 deletions
| @@ -71,7 +71,8 @@ class TableBorder extends Border { | @@ -71,7 +71,8 @@ class TableBorder extends Border { | ||
| 71 | bottom: side, | 71 | bottom: side, |
| 72 | left: side, | 72 | left: side, |
| 73 | horizontalInside: side, | 73 | horizontalInside: side, |
| 74 | - verticalInside: side); | 74 | + verticalInside: side, |
| 75 | + ); | ||
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | /// Creates a border for a table where all the interior sides use the same styling and all the exterior sides use the same styling. | 78 | /// 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 { | @@ -149,8 +150,8 @@ class TableContext extends WidgetContext { | ||
| 149 | class ColumnLayout { | 150 | class ColumnLayout { |
| 150 | ColumnLayout(this.width, this.flex); | 151 | ColumnLayout(this.width, this.flex); |
| 151 | 152 | ||
| 152 | - final double? width; | ||
| 153 | - final double? flex; | 153 | + final double width; |
| 154 | + final double flex; | ||
| 154 | } | 155 | } |
| 155 | 156 | ||
| 156 | abstract class TableColumnWidth { | 157 | abstract class TableColumnWidth { |
| @@ -167,9 +168,12 @@ class IntrinsicColumnWidth extends TableColumnWidth { | @@ -167,9 +168,12 @@ class IntrinsicColumnWidth extends TableColumnWidth { | ||
| 167 | 168 | ||
| 168 | @override | 169 | @override |
| 169 | ColumnLayout layout( | 170 | ColumnLayout layout( |
| 170 | - Widget child, Context context, BoxConstraints constraints) { | 171 | + Widget child, |
| 172 | + Context context, | ||
| 173 | + BoxConstraints constraints, | ||
| 174 | + ) { | ||
| 171 | if (flex != null) { | 175 | if (flex != null) { |
| 172 | - return ColumnLayout(0, flex); | 176 | + return ColumnLayout(0, flex!); |
| 173 | } | 177 | } |
| 174 | 178 | ||
| 175 | child.layout(context, const BoxConstraints()); | 179 | child.layout(context, const BoxConstraints()); |
| @@ -315,7 +319,7 @@ class Table extends Widget with SpanningWidget { | @@ -315,7 +319,7 @@ class Table extends Widget with SpanningWidget { | ||
| 315 | 319 | ||
| 316 | final TableWidth tableWidth; | 320 | final TableWidth tableWidth; |
| 317 | 321 | ||
| 318 | - final List<double?> _widths = <double?>[]; | 322 | + final List<double> _widths = <double>[]; |
| 319 | final List<double> _heights = <double>[]; | 323 | final List<double> _heights = <double>[]; |
| 320 | 324 | ||
| 321 | final TableContext _context = TableContext(); | 325 | final TableContext _context = TableContext(); |
| @@ -338,28 +342,27 @@ class Table extends Widget with SpanningWidget { | @@ -338,28 +342,27 @@ class Table extends Widget with SpanningWidget { | ||
| 338 | void layout(Context context, BoxConstraints constraints, | 342 | void layout(Context context, BoxConstraints constraints, |
| 339 | {bool parentUsesSize = false}) { | 343 | {bool parentUsesSize = false}) { |
| 340 | // Compute required width for all row/columns width flex | 344 | // Compute required width for all row/columns width flex |
| 341 | - final flex = <double?>[]; | 345 | + final flex = <double>[]; |
| 342 | _widths.clear(); | 346 | _widths.clear(); |
| 343 | _heights.clear(); | 347 | _heights.clear(); |
| 344 | var index = 0; | 348 | var index = 0; |
| 345 | 349 | ||
| 346 | for (final row in children) { | 350 | for (final row in children) { |
| 347 | - var n = 0; | ||
| 348 | - for (final child in row.children) { | ||
| 349 | - final columnWidth = columnWidths != null && columnWidths![n] != null | ||
| 350 | - ? columnWidths![n]! | ||
| 351 | - : defaultColumnWidth; | 351 | + for (final entry in row.children.asMap().entries) { |
| 352 | + final index = entry.key; | ||
| 353 | + final child = entry.value; | ||
| 354 | + final columnWidth = columnWidths?[index] ?? defaultColumnWidth; | ||
| 352 | final columnLayout = columnWidth.layout(child, context, constraints); | 355 | final columnLayout = columnWidth.layout(child, context, constraints); |
| 353 | - if (flex.length < n + 1) { | 356 | + |
| 357 | + if (index >= flex.length) { | ||
| 354 | flex.add(columnLayout.flex); | 358 | flex.add(columnLayout.flex); |
| 355 | _widths.add(columnLayout.width); | 359 | _widths.add(columnLayout.width); |
| 356 | } else { | 360 | } else { |
| 357 | - if (columnLayout.flex! > 0) { | ||
| 358 | - flex[n] = math.max(flex[n]!, columnLayout.flex!); | 361 | + if (columnLayout.flex > 0) { |
| 362 | + flex[index] = math.max(flex[index], columnLayout.flex); | ||
| 359 | } | 363 | } |
| 360 | - _widths[n] = math.max(_widths[n]!, columnLayout.width!); | 364 | + _widths[index] = math.max(_widths[index], columnLayout.width); |
| 361 | } | 365 | } |
| 362 | - n++; | ||
| 363 | } | 366 | } |
| 364 | } | 367 | } |
| 365 | 368 | ||
| @@ -368,20 +371,20 @@ class Table extends Widget with SpanningWidget { | @@ -368,20 +371,20 @@ class Table extends Widget with SpanningWidget { | ||
| 368 | return; | 371 | return; |
| 369 | } | 372 | } |
| 370 | 373 | ||
| 371 | - final maxWidth = _widths.reduce((double? a, double? b) => a! + b!); | 374 | + final maxWidth = _widths.fold(0.0, (sum, element) => sum + element); |
| 372 | 375 | ||
| 373 | // Compute column widths using flex and estimated width | 376 | // Compute column widths using flex and estimated width |
| 374 | if (constraints.hasBoundedWidth) { | 377 | if (constraints.hasBoundedWidth) { |
| 375 | - final totalFlex = flex.reduce((double? a, double? b) => a! + b!)!; | 378 | + final totalFlex = flex.reduce((double? a, double? b) => a! + b!); |
| 376 | var flexSpace = 0.0; | 379 | var flexSpace = 0.0; |
| 377 | for (var n = 0; n < _widths.length; n++) { | 380 | for (var n = 0; n < _widths.length; n++) { |
| 378 | if (flex[n] == 0.0) { | 381 | if (flex[n] == 0.0) { |
| 379 | - final newWidth = _widths[n]! / maxWidth! * constraints.maxWidth; | 382 | + final newWidth = _widths[n] / maxWidth * constraints.maxWidth; |
| 380 | if ((tableWidth == TableWidth.max && totalFlex == 0.0) || | 383 | if ((tableWidth == TableWidth.max && totalFlex == 0.0) || |
| 381 | - newWidth < _widths[n]!) { | 384 | + newWidth < _widths[n]) { |
| 382 | _widths[n] = newWidth; | 385 | _widths[n] = newWidth; |
| 383 | } | 386 | } |
| 384 | - flexSpace += _widths[n]!; | 387 | + flexSpace += _widths[n]; |
| 385 | } | 388 | } |
| 386 | } | 389 | } |
| 387 | final spacePerFlex = totalFlex > 0.0 | 390 | final spacePerFlex = totalFlex > 0.0 |
| @@ -389,14 +392,14 @@ class Table extends Widget with SpanningWidget { | @@ -389,14 +392,14 @@ class Table extends Widget with SpanningWidget { | ||
| 389 | : double.nan; | 392 | : double.nan; |
| 390 | 393 | ||
| 391 | for (var n = 0; n < _widths.length; n++) { | 394 | for (var n = 0; n < _widths.length; n++) { |
| 392 | - if (flex[n]! > 0.0) { | ||
| 393 | - final newWidth = spacePerFlex * flex[n]!; | 395 | + if (flex[n] > 0.0) { |
| 396 | + final newWidth = spacePerFlex * flex[n]; | ||
| 394 | _widths[n] = newWidth; | 397 | _widths[n] = newWidth; |
| 395 | } | 398 | } |
| 396 | } | 399 | } |
| 397 | } | 400 | } |
| 398 | 401 | ||
| 399 | - final totalWidth = _widths.reduce((double? a, double? b) => a! + b!)!; | 402 | + final totalWidth = _widths.fold(0.0, (sum, element) => sum + element); |
| 400 | 403 | ||
| 401 | // Compute final widths | 404 | // Compute final widths |
| 402 | var totalHeight = 0.0; | 405 | var totalHeight = 0.0; |
| @@ -416,7 +419,7 @@ class Table extends Widget with SpanningWidget { | @@ -416,7 +419,7 @@ class Table extends Widget with SpanningWidget { | ||
| 416 | assert(child.box != null); | 419 | assert(child.box != null); |
| 417 | child.box = | 420 | child.box = |
| 418 | PdfRect(x, totalHeight, child.box!.width, child.box!.height); | 421 | PdfRect(x, totalHeight, child.box!.width, child.box!.height); |
| 419 | - x += _widths[n]!; | 422 | + x += _widths[n]; |
| 420 | lineHeight = math.max(lineHeight, child.box!.height); | 423 | lineHeight = math.max(lineHeight, child.box!.height); |
| 421 | n++; | 424 | n++; |
| 422 | } | 425 | } |
| @@ -434,7 +437,7 @@ class Table extends Widget with SpanningWidget { | @@ -434,7 +437,7 @@ class Table extends Widget with SpanningWidget { | ||
| 434 | assert(child.box != null); | 437 | assert(child.box != null); |
| 435 | child.box = | 438 | child.box = |
| 436 | PdfRect(x, totalHeight, child.box!.width, child.box!.height); | 439 | PdfRect(x, totalHeight, child.box!.width, child.box!.height); |
| 437 | - x += _widths[n]!; | 440 | + x += _widths[n]; |
| 438 | n++; | 441 | n++; |
| 439 | } | 442 | } |
| 440 | } | 443 | } |
-
Please register or login to post a comment