@@ -336,6 +336,10 @@ class RenderTagflowTable extends RenderBox
336336 Color ? _headerBackgroundColor;
337337 EdgeInsets _padding = EdgeInsets .zero;
338338 IndexedWidgetBuilder ? _separatorBuilder;
339+ double _columnSpacing = 0 ;
340+ double _rowSpacing = 0 ;
341+
342+ static const double _minimumColumnWidth = 40 ; // Adjust as needed
339343
340344 void setSeparatorBuilder (IndexedWidgetBuilder ? value) {
341345 if (_separatorBuilder == value) return ;
@@ -378,6 +382,18 @@ class RenderTagflowTable extends RenderBox
378382 markNeedsPaint ();
379383 }
380384
385+ void setColumnSpacing (double value) {
386+ if (_columnSpacing == value) return ;
387+ _columnSpacing = value;
388+ markNeedsLayout ();
389+ }
390+
391+ void setRowSpacing (double value) {
392+ if (_rowSpacing == value) return ;
393+ _rowSpacing = value;
394+ markNeedsLayout ();
395+ }
396+
381397 @override
382398 void setupParentData (RenderBox child) {
383399 if (child.parentData is ! TableCellData ) {
@@ -392,6 +408,12 @@ class RenderTagflowTable extends RenderBox
392408 return ;
393409 }
394410
411+ // Calculate total spacing needed between columns
412+ final totalColumnSpacing = (_columnCount - 1 ) * _columnSpacing;
413+
414+ // Calculate available width for columns after accounting for spacing and padding
415+ final availableWidth = constraints.maxWidth - totalColumnSpacing - _padding.horizontal;
416+
395417 // First pass: Calculate minimum and preferred column widths
396418 _columnWidths = List <double >.filled (_columnCount, 0 );
397419 final columnFlexibility = List <double >.filled (_columnCount, 0 );
@@ -407,39 +429,47 @@ class RenderTagflowTable extends RenderBox
407429 // Update minimum widths
408430 for (var i = 0 ; i < childParentData.colSpan; i++ ) {
409431 final colIndex = childParentData.column + i;
410- _columnWidths[colIndex] =
411- _columnWidths[colIndex].clamp (widthPerColumn, double .infinity);
412-
432+ _columnWidths[colIndex] = math.max (_columnWidths[colIndex], widthPerColumn);
433+
413434 // Track how flexible each column is based on content
414435 final flexibility = (maxWidth - minWidth) / childParentData.colSpan;
415- columnFlexibility[colIndex] =
416- math.max (columnFlexibility[colIndex], flexibility);
436+ columnFlexibility[colIndex] = math.max (columnFlexibility[colIndex], flexibility);
417437 }
418438 }
419439 child = childParentData.nextSibling;
420440 }
421441
422- // Calculate total minimum width and distribute extra space
423- final totalMinWidth =
424- _columnWidths.reduce ((a, b) => a + b) + _padding.horizontal;
425- final extraWidth =
426- (constraints.maxWidth - totalMinWidth).clamp (0.0 , double .infinity);
427-
428- if (extraWidth > 0 ) {
429- // Calculate total flexibility
442+ // Calculate total current width and adjust if needed
443+ final totalColumnWidth = _columnWidths.reduce ((a, b) => a + b);
444+
445+ if (totalColumnWidth > availableWidth) {
446+ // Need to shrink columns - distribute reduction proportionally
447+ final reduction = totalColumnWidth - availableWidth;
448+
449+ // Calculate shrink factors based on current widths
450+ final totalWidth = _columnWidths.reduce ((a, b) => a + b);
451+ for (var i = 0 ; i < _columnCount; i++ ) {
452+ final proportion = _columnWidths[i] / totalWidth;
453+ _columnWidths[i] = math.max (
454+ _minimumColumnWidth,
455+ _columnWidths[i] - (reduction * proportion),
456+ );
457+ }
458+ } else if (totalColumnWidth < availableWidth) {
459+ // Extra space available - distribute using flexibility
460+ final extraWidth = availableWidth - totalColumnWidth;
430461 final totalFlexibility = columnFlexibility.reduce ((a, b) => a + b);
431462
432463 if (totalFlexibility > 0 ) {
433- // Distribute extra space proportionally to column flexibility
434464 for (var i = 0 ; i < _columnCount; i++ ) {
435465 final proportion = columnFlexibility[i] / totalFlexibility;
436466 _columnWidths[i] += extraWidth * proportion;
437467 }
438468 } else {
439- // If no flexible columns found , distribute evenly
440- final widthPerColumn = extraWidth / _columnCount;
469+ // If no flexible columns, distribute evenly
470+ final extraPerColumn = extraWidth / _columnCount;
441471 for (var i = 0 ; i < _columnCount; i++ ) {
442- _columnWidths[i] += widthPerColumn ;
472+ _columnWidths[i] += extraPerColumn ;
443473 }
444474 }
445475 }
@@ -489,26 +519,32 @@ class RenderTagflowTable extends RenderBox
489519 }
490520 childParentData.offset = Offset (0 , y);
491521 } else {
492- // Calculate cell width and height
522+ // Calculate cell width and height including spacing
493523 var width = 0.0 ;
494524 for (var i = 0 ; i < childParentData.colSpan; i++ ) {
495525 width += _columnWidths[childParentData.column + i];
526+ if (i < childParentData.colSpan - 1 ) {
527+ width += _columnSpacing;
528+ }
496529 }
497530
498531 var height = 0.0 ;
499532 for (var i = 0 ; i < childParentData.rowSpan; i++ ) {
500533 height += _rowHeights[childParentData.row + i];
534+ if (i < childParentData.rowSpan - 1 ) {
535+ height += _rowSpacing;
536+ }
501537 }
502538
503- // Calculate cell position
539+ // Calculate cell position with spacing
504540 var x = _padding.left;
505541 for (var i = 0 ; i < childParentData.column; i++ ) {
506- x += _columnWidths[i];
542+ x += _columnWidths[i] + _columnSpacing ;
507543 }
508544
509545 var y = _padding.top;
510546 for (var i = 0 ; i < childParentData.row; i++ ) {
511- y += _rowHeights[i];
547+ y += _rowHeights[i] + _rowSpacing ;
512548 }
513549
514550 // Layout child with tight constraints to prevent overflow
@@ -519,13 +555,15 @@ class RenderTagflowTable extends RenderBox
519555 child = childParentData.nextSibling;
520556 }
521557
522- // Set table size
523- size = constraints.constrain (
524- Size (
525- _columnWidths.reduce ((a, b) => a + b) + _padding.horizontal,
526- _rowHeights.reduce ((a, b) => a + b) + _padding.vertical,
527- ),
528- );
558+ // Update final table size to include spacing
559+ final tableWidth = _columnWidths.reduce ((a, b) => a + b) +
560+ (_columnCount - 1 ) * _columnSpacing +
561+ _padding.horizontal;
562+ final tableHeight = _rowHeights.reduce ((a, b) => a + b) +
563+ (_rowCount - 1 ) * _rowSpacing +
564+ _padding.vertical;
565+
566+ size = constraints.constrain (Size (tableWidth, tableHeight));
529567 }
530568
531569 @override
@@ -753,6 +791,7 @@ class RenderTagflowTable extends RenderBox
753791 rowHeights[childParentData.row + i]
754792 .clamp (childHeight, double .infinity);
755793 }
794+ child = childParentData.nextSibling;
756795 }
757796 child = childParentData.nextSibling;
758797 }
0 commit comments