@@ -30,6 +30,7 @@ class TableRow {
30
30
this .repeat = false ,
31
31
this .verticalAlignment,
32
32
this .decoration,
33
+ this .columnSpans,
33
34
});
34
35
35
36
/// The widgets that comprise the cells in this row.
@@ -41,6 +42,8 @@ class TableRow {
41
42
final BoxDecoration ? decoration;
42
43
43
44
final TableCellVerticalAlignment ? verticalAlignment;
45
+
46
+ final Map <int , int >? columnSpans;
44
47
}
45
48
46
49
enum TableCellVerticalAlignment { bottom, middle, top, full }
@@ -334,69 +337,103 @@ class Table extends Widget with SpanningWidget {
334
337
_context.firstLine = _context.lastLine;
335
338
}
336
339
340
+ List <Widget > _getFilledChildrenFromColumnSpans (TableRow row) {
341
+ if (row.columnSpans == null ) {
342
+ return row.children;
343
+ }
344
+ final filledChildren = < Widget > [];
345
+ var n = 0 ;
346
+ // TODO(Gustl22): Handle intrinsic column widths
347
+ for (final child in row.children) {
348
+ // Columns, which are currently spanned.
349
+ final columnSpan = row.columnSpans! [n] ?? 1 ;
350
+ filledChildren.add (child);
351
+ if (columnSpan > 1 ) {
352
+ filledChildren
353
+ .addAll (Iterable .generate (columnSpan - 1 , (index) => Container ()));
354
+ }
355
+ n += columnSpan;
356
+ }
357
+ return filledChildren;
358
+ }
359
+
360
+ List <double > _getSpannedWidths (List <double ?> widths, TableRow row) {
361
+ final spannedWidths = < double > [];
362
+ var n = 0 ;
363
+ for (var i = 0 ; i < row.children.length; i++ ) {
364
+ final columnSpan = row.columnSpans? [n] ?? 1 ;
365
+ final indices = Iterable .generate (columnSpan, (span) => n + span);
366
+ final width =
367
+ indices.fold (0.0 , (prev, curIndex) => prev + (widths[curIndex] ?? 0 ));
368
+ spannedWidths.add (width);
369
+ n += columnSpan;
370
+ }
371
+ return spannedWidths;
372
+ }
373
+
337
374
@override
338
375
void layout (Context context, BoxConstraints constraints,
339
376
{bool parentUsesSize = false }) {
340
377
// Compute required width for all row/columns width flex
341
378
final flex = < double ? > [];
342
- _widths. clear () ;
379
+ final widths = < double ? > [] ;
343
380
_heights.clear ();
344
381
var index = 0 ;
345
382
346
383
for (final row in children) {
347
384
var n = 0 ;
348
- for (final child in row.children ) {
385
+ for (final child in _getFilledChildrenFromColumnSpans ( row) ) {
349
386
final columnWidth = columnWidths != null && columnWidths! [n] != null
350
387
? columnWidths! [n]!
351
388
: defaultColumnWidth;
352
389
final columnLayout = columnWidth.layout (child, context, constraints);
353
390
if (flex.length < n + 1 ) {
354
391
flex.add (columnLayout.flex);
355
- _widths .add (columnLayout.width);
392
+ widths .add (columnLayout.width);
356
393
} else {
357
394
if (columnLayout.flex! > 0 ) {
358
395
flex[n] = math.max (flex[n]! , columnLayout.flex! );
359
396
}
360
- _widths [n] = math.max (_widths [n]! , columnLayout.width! );
397
+ widths [n] = math.max (widths [n]! , columnLayout.width! );
361
398
}
362
399
n++ ;
363
400
}
364
401
}
365
402
366
- if (_widths .isEmpty) {
403
+ if (widths .isEmpty) {
367
404
box = PdfRect .fromPoints (PdfPoint .zero, constraints.smallest);
368
405
return ;
369
406
}
370
407
371
- final maxWidth = _widths .reduce ((double ? a, double ? b) => a! + b! );
408
+ final maxWidth = widths .reduce ((double ? a, double ? b) => a! + b! );
372
409
373
410
// Compute column widths using flex and estimated width
374
411
if (constraints.hasBoundedWidth) {
375
412
final totalFlex = flex.reduce ((double ? a, double ? b) => a! + b! )! ;
376
413
var flexSpace = 0.0 ;
377
- for (var n = 0 ; n < _widths .length; n++ ) {
414
+ for (var n = 0 ; n < widths .length; n++ ) {
378
415
if (flex[n] == 0.0 ) {
379
- final newWidth = _widths [n]! / maxWidth! * constraints.maxWidth;
416
+ final newWidth = widths [n]! / maxWidth! * constraints.maxWidth;
380
417
if ((tableWidth == TableWidth .max && totalFlex == 0.0 ) ||
381
- newWidth < _widths [n]! ) {
382
- _widths [n] = newWidth;
418
+ newWidth < widths [n]! ) {
419
+ widths [n] = newWidth;
383
420
}
384
- flexSpace += _widths [n]! ;
421
+ flexSpace += widths [n]! ;
385
422
}
386
423
}
387
424
final spacePerFlex = totalFlex > 0.0
388
425
? ((constraints.maxWidth - flexSpace) / totalFlex)
389
426
: double .nan;
390
427
391
- for (var n = 0 ; n < _widths .length; n++ ) {
428
+ for (var n = 0 ; n < widths .length; n++ ) {
392
429
if (flex[n]! > 0.0 ) {
393
430
final newWidth = spacePerFlex * flex[n]! ;
394
- _widths [n] = newWidth;
431
+ widths [n] = newWidth;
395
432
}
396
433
}
397
434
}
398
435
399
- final totalWidth = _widths .reduce ((double ? a, double ? b) => a! + b! )! ;
436
+ final totalWidth = widths .reduce ((double ? a, double ? b) => a! + b! )! ;
400
437
401
438
// Compute final widths
402
439
var totalHeight = 0.0 ;
@@ -406,17 +443,21 @@ class Table extends Widget with SpanningWidget {
406
443
continue ;
407
444
}
408
445
446
+ final spannedWidths = _getSpannedWidths (widths, row);
447
+
409
448
var n = 0 ;
410
449
var x = 0.0 ;
411
450
412
451
var lineHeight = 0.0 ;
452
+
413
453
for (final child in row.children) {
414
- final childConstraints = BoxConstraints .tightFor (width: _widths[n]);
454
+ final childConstraints =
455
+ BoxConstraints .tightFor (width: spannedWidths[n]);
415
456
child.layout (context, childConstraints);
416
457
assert (child.box != null );
417
458
child.box =
418
459
PdfRect (x, totalHeight, child.box! .width, child.box! .height);
419
- x += _widths [n]! ;
460
+ x += spannedWidths [n];
420
461
lineHeight = math.max (lineHeight, child.box! .height);
421
462
n++ ;
422
463
}
@@ -428,13 +469,13 @@ class Table extends Widget with SpanningWidget {
428
469
n = 0 ;
429
470
x = 0 ;
430
471
for (final child in row.children) {
431
- final childConstraints =
432
- BoxConstraints . tightFor ( width: _widths [n], height: lineHeight);
472
+ final childConstraints = BoxConstraints . tightFor (
473
+ width: spannedWidths [n], height: lineHeight);
433
474
child.layout (context, childConstraints);
434
475
assert (child.box != null );
435
476
child.box =
436
477
PdfRect (x, totalHeight, child.box! .width, child.box! .height);
437
- x += _widths [n]! ;
478
+ x += spannedWidths [n];
438
479
n++ ;
439
480
}
440
481
}
@@ -491,6 +532,9 @@ class Table extends Widget with SpanningWidget {
491
532
}
492
533
493
534
box = PdfRect (0 , 0 , totalWidth, totalHeight);
535
+
536
+ _widths.clear ();
537
+ _widths.addAll (widths);
494
538
}
495
539
496
540
@override
0 commit comments