Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2025-01-30

### Changes

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`tagflow_table` - `v0.0.3-dev.0`](#tagflow_table---v003-dev0)

---

#### `tagflow_table` - `v0.0.3-dev.0`

- **FEAT**(tagflow_table): add column and row spacing properties to TagflowTable. ([66989cf5](https://github.com/devaryakjha/tagflow/commit/66989cf5e67805c2b472dcfccd9bed84158fdf8d))


## 2025-01-30

### Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/tagflow/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
go_router: ^14.6.2
google_fonts: ^6.2.1
tagflow: ^0.0.1-dev.0+1
tagflow_table: ^0.0.2-dev.0+1
tagflow_table: ^0.0.3-dev.0
url_launcher: ^6.3.1

dev_dependencies:
Expand Down
4 changes: 4 additions & 0 deletions packages/tagflow_table/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.3-dev.0

- **FEAT**(tagflow_table): add column and row spacing properties to TagflowTable. ([66989cf5](https://github.com/devaryakjha/tagflow/commit/66989cf5e67805c2b472dcfccd9bed84158fdf8d))

## 0.0.2+1

- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
Expand Down
95 changes: 67 additions & 28 deletions packages/tagflow_table/lib/src/rendering/tagflow_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class RenderTagflowTable extends RenderBox
Color? _headerBackgroundColor;
EdgeInsets _padding = EdgeInsets.zero;
IndexedWidgetBuilder? _separatorBuilder;
double _columnSpacing = 0;
double _rowSpacing = 0;

static const double _minimumColumnWidth = 40; // Adjust as needed

void setSeparatorBuilder(IndexedWidgetBuilder? value) {
if (_separatorBuilder == value) return;
Expand Down Expand Up @@ -378,6 +382,18 @@ class RenderTagflowTable extends RenderBox
markNeedsPaint();
}

void setColumnSpacing(double value) {
if (_columnSpacing == value) return;
_columnSpacing = value;
markNeedsLayout();
}

void setRowSpacing(double value) {
if (_rowSpacing == value) return;
_rowSpacing = value;
markNeedsLayout();
}

@override
void setupParentData(RenderBox child) {
if (child.parentData is! TableCellData) {
Expand All @@ -392,6 +408,12 @@ class RenderTagflowTable extends RenderBox
return;
}

// Calculate total spacing needed between columns
final totalColumnSpacing = (_columnCount - 1) * _columnSpacing;

// Calculate available width for columns after accounting for spacing and padding
final availableWidth = constraints.maxWidth - totalColumnSpacing - _padding.horizontal;

// First pass: Calculate minimum and preferred column widths
_columnWidths = List<double>.filled(_columnCount, 0);
final columnFlexibility = List<double>.filled(_columnCount, 0);
Expand All @@ -407,39 +429,47 @@ class RenderTagflowTable extends RenderBox
// Update minimum widths
for (var i = 0; i < childParentData.colSpan; i++) {
final colIndex = childParentData.column + i;
_columnWidths[colIndex] =
_columnWidths[colIndex].clamp(widthPerColumn, double.infinity);

_columnWidths[colIndex] = math.max(_columnWidths[colIndex], widthPerColumn);

// Track how flexible each column is based on content
final flexibility = (maxWidth - minWidth) / childParentData.colSpan;
columnFlexibility[colIndex] =
math.max(columnFlexibility[colIndex], flexibility);
columnFlexibility[colIndex] = math.max(columnFlexibility[colIndex], flexibility);
}
}
child = childParentData.nextSibling;
}

// Calculate total minimum width and distribute extra space
final totalMinWidth =
_columnWidths.reduce((a, b) => a + b) + _padding.horizontal;
final extraWidth =
(constraints.maxWidth - totalMinWidth).clamp(0.0, double.infinity);

if (extraWidth > 0) {
// Calculate total flexibility
// Calculate total current width and adjust if needed
final totalColumnWidth = _columnWidths.reduce((a, b) => a + b);

if (totalColumnWidth > availableWidth) {
// Need to shrink columns - distribute reduction proportionally
final reduction = totalColumnWidth - availableWidth;

// Calculate shrink factors based on current widths
final totalWidth = _columnWidths.reduce((a, b) => a + b);
for (var i = 0; i < _columnCount; i++) {
final proportion = _columnWidths[i] / totalWidth;
_columnWidths[i] = math.max(
_minimumColumnWidth,
_columnWidths[i] - (reduction * proportion),
);
}
} else if (totalColumnWidth < availableWidth) {
// Extra space available - distribute using flexibility
final extraWidth = availableWidth - totalColumnWidth;
final totalFlexibility = columnFlexibility.reduce((a, b) => a + b);

if (totalFlexibility > 0) {
// Distribute extra space proportionally to column flexibility
for (var i = 0; i < _columnCount; i++) {
final proportion = columnFlexibility[i] / totalFlexibility;
_columnWidths[i] += extraWidth * proportion;
}
} else {
// If no flexible columns found, distribute evenly
final widthPerColumn = extraWidth / _columnCount;
// If no flexible columns, distribute evenly
final extraPerColumn = extraWidth / _columnCount;
for (var i = 0; i < _columnCount; i++) {
_columnWidths[i] += widthPerColumn;
_columnWidths[i] += extraPerColumn;
}
}
}
Expand Down Expand Up @@ -489,26 +519,32 @@ class RenderTagflowTable extends RenderBox
}
childParentData.offset = Offset(0, y);
} else {
// Calculate cell width and height
// Calculate cell width and height including spacing
var width = 0.0;
for (var i = 0; i < childParentData.colSpan; i++) {
width += _columnWidths[childParentData.column + i];
if (i < childParentData.colSpan - 1) {
width += _columnSpacing;
}
}

var height = 0.0;
for (var i = 0; i < childParentData.rowSpan; i++) {
height += _rowHeights[childParentData.row + i];
if (i < childParentData.rowSpan - 1) {
height += _rowSpacing;
}
}

// Calculate cell position
// Calculate cell position with spacing
var x = _padding.left;
for (var i = 0; i < childParentData.column; i++) {
x += _columnWidths[i];
x += _columnWidths[i] + _columnSpacing;
}

var y = _padding.top;
for (var i = 0; i < childParentData.row; i++) {
y += _rowHeights[i];
y += _rowHeights[i] + _rowSpacing;
}

// Layout child with tight constraints to prevent overflow
Expand All @@ -519,13 +555,15 @@ class RenderTagflowTable extends RenderBox
child = childParentData.nextSibling;
}

// Set table size
size = constraints.constrain(
Size(
_columnWidths.reduce((a, b) => a + b) + _padding.horizontal,
_rowHeights.reduce((a, b) => a + b) + _padding.vertical,
),
);
// Update final table size to include spacing
final tableWidth = _columnWidths.reduce((a, b) => a + b) +
(_columnCount - 1) * _columnSpacing +
_padding.horizontal;
final tableHeight = _rowHeights.reduce((a, b) => a + b) +
(_rowCount - 1) * _rowSpacing +
_padding.vertical;

size = constraints.constrain(Size(tableWidth, tableHeight));
}

@override
Expand Down Expand Up @@ -753,6 +791,7 @@ class RenderTagflowTable extends RenderBox
rowHeights[childParentData.row + i]
.clamp(childHeight, double.infinity);
}
child = childParentData.nextSibling;
}
child = childParentData.nextSibling;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/tagflow_table/lib/src/widgets/converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ final class TagflowTableConverter
this.headerBackgroundColor,
this.treatFirstRowAsHeader = false,
this.separatorBuilder,
this.columnSpacing,
this.rowSpacing,
});

/// The background color of the header row.
Expand All @@ -47,6 +49,11 @@ final class TagflowTableConverter
/// The separator is placed between each row.
final material.IndexedWidgetBuilder? separatorBuilder;

final double? columnSpacing;

///
final double? rowSpacing;

@override
Set<String> get supportedTags => {'table'};

Expand Down Expand Up @@ -171,6 +178,8 @@ final class TagflowTableConverter
headerBackgroundColor: headerBackgroundColor,
padding: style.padding ?? material.EdgeInsets.zero,
separatorBuilder: separatorBuilder,
columnSpacing: columnSpacing ?? 0,
rowSpacing: rowSpacing ?? 0,
children: cells,
),
);
Expand Down
16 changes: 13 additions & 3 deletions packages/tagflow_table/lib/src/widgets/tagflow_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ final class TagflowTable extends MultiChildRenderObjectWidget {
this.headerBackgroundColor,
this.padding = EdgeInsets.zero,
this.separatorBuilder,
this.columnSpacing = 0.0, // Add this
this.rowSpacing = 0.0, // Add this
}) : border = border ?? TagflowTableBorder.none,
super(children: children);

Expand All @@ -82,6 +84,8 @@ final class TagflowTable extends MultiChildRenderObjectWidget {
final Color? headerBackgroundColor;
final EdgeInsets padding;
final IndexedWidgetBuilder? separatorBuilder;
final double columnSpacing;
final double rowSpacing;

@override
RenderTagflowTable createRenderObject(BuildContext context) {
Expand All @@ -91,7 +95,9 @@ final class TagflowTable extends MultiChildRenderObjectWidget {
..setTreatFirstRowAsHeader(value: treatFirstRowAsHeader)
..setHeaderBackgroundColor(headerBackgroundColor)
..setSeparatorBuilder(separatorBuilder)
..setPadding(padding);
..setPadding(padding)
..setColumnSpacing(columnSpacing) // Add this
..setRowSpacing(rowSpacing); // Add this
}

@override
Expand All @@ -105,7 +111,9 @@ final class TagflowTable extends MultiChildRenderObjectWidget {
..setTreatFirstRowAsHeader(value: treatFirstRowAsHeader)
..setHeaderBackgroundColor(headerBackgroundColor)
..setSeparatorBuilder(separatorBuilder)
..setPadding(padding);
..setPadding(padding)
..setColumnSpacing(columnSpacing) // Add this
..setRowSpacing(rowSpacing); // Add this
}

@override
Expand All @@ -128,6 +136,8 @@ final class TagflowTable extends MultiChildRenderObjectWidget {
headerBackgroundColor,
),
)
..add(DiagnosticsProperty<EdgeInsets>('padding', padding));
..add(DiagnosticsProperty<EdgeInsets>('padding', padding))
..add(DoubleProperty('columnSpacing', columnSpacing))
..add(DoubleProperty('rowSpacing', rowSpacing));
}
}
2 changes: 1 addition & 1 deletion packages/tagflow_table/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: tagflow_table
description: A pluggable package for tagflow to add support for advanced HTML tables.
version: 0.0.2+1
version: 0.0.3-dev.0
repository: https://github.com/devaryakjha/tagflow
homepage: https://docs.arya.run/tagflow
issue_tracker: https://github.com/devaryakjha/tagflow/issues
Expand Down