From 1a449652a04365b8367ac6466935b22fed3aa73c Mon Sep 17 00:00:00 2001 From: Yelzhan Yerkebulan <33483071+yerkejs@users.noreply.github.com> Date: Tue, 22 Jul 2025 01:44:36 +0500 Subject: [PATCH 1/3] Add shouldRenderColumnsLazy --- lib/src/sliver_table_view.dart | 2 ++ lib/src/table_content_layout.dart | 4 +++- lib/src/table_view.dart | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/src/sliver_table_view.dart b/lib/src/sliver_table_view.dart index d58a533..288d7a6 100644 --- a/lib/src/sliver_table_view.dart +++ b/lib/src/sliver_table_view.dart @@ -51,6 +51,7 @@ class SliverTableView extends TableView { super.footerBuilder, super.footerHeight, super.physicsHorizontal, + super.shouldRenderColumnsLazy, }) : super.builder(physics: null); /// A scroll controller used for the horizontal scrolling of the table. @@ -149,6 +150,7 @@ class _SliverTableViewState extends State columns: columns, scrollPadding: scrollPadding, child: TableContentLayout( + shouldRenderColumnsLazy: widget.shouldRenderColumnsLazy, verticalDividersStyle: style.dividers.vertical, width: width, fixedRowHeight: true, diff --git a/lib/src/table_content_layout.dart b/lib/src/table_content_layout.dart index 41435ba..53b43e6 100644 --- a/lib/src/table_content_layout.dart +++ b/lib/src/table_content_layout.dart @@ -24,6 +24,7 @@ class TableContentLayout extends StatefulWidget { final double minScrollableWidthRatio; final TextDirection textDirection; final EdgeInsets scrollPadding; + final bool shouldRenderColumnsLazy; final Widget child; const TableContentLayout({ @@ -39,6 +40,7 @@ class TableContentLayout extends StatefulWidget { required this.textDirection, required this.scrollPadding, required this.child, + required this.shouldRenderColumnsLazy, }); static TableContentLayoutData of(BuildContext context) { @@ -223,7 +225,7 @@ class TableContentLayoutState extends State ? column.width + widget.scrollPadding.right : 0) <= widget.width) { - if (centerOffset >= -column.width) { + if (widget.shouldRenderColumnsLazy && centerOffset >= -column.width) { columnsCenter.add(i); columnOffsetsCenter.add(centerOffset); } diff --git a/lib/src/table_view.dart b/lib/src/table_view.dart index 57f187d..800badd 100644 --- a/lib/src/table_view.dart +++ b/lib/src/table_view.dart @@ -56,6 +56,7 @@ class TableView extends StatefulWidget { this.physicsHorizontal, this.shrinkWrapVertical = false, this.shrinkWrapHorizontal = false, + this.shouldRenderColumnsLazy = true, }) : assert(rowCount >= 0), assert(rowHeight == null || rowHeight > 0), assert(headerHeight == null || headerHeight > 0), @@ -308,6 +309,13 @@ class TableView extends StatefulWidget { /// Defaults to false. final bool shrinkWrapHorizontal; + /// Whether to render columns lazily. + /// + /// If true, columns will be rendered only when they are visible. + /// + /// Defaults to true. + final bool shouldRenderColumnsLazy; + @override State createState() => _TableViewState(); } @@ -416,6 +424,7 @@ class _TableViewState extends State horizontalOffset: horizontalOffset, stickyHorizontalOffset: _stickyHorizontalOffset, minScrollableWidth: widget.minScrollableWidth, + shouldRenderColumnsLazy: widget.shouldRenderColumnsLazy, child: Builder( builder: (context) => TableScaffold( shrinkWrapVertical: widget.shrinkWrapVertical, From b173260c35d687812b1cb956ccde881d6f8effd6 Mon Sep 17 00:00:00 2001 From: Yelzhan Yerkebulan <33483071+yerkejs@users.noreply.github.com> Date: Tue, 22 Jul 2025 02:20:00 +0500 Subject: [PATCH 2/3] Fix condition --- lib/src/table_content_layout.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/table_content_layout.dart b/lib/src/table_content_layout.dart index 53b43e6..5d5e2d4 100644 --- a/lib/src/table_content_layout.dart +++ b/lib/src/table_content_layout.dart @@ -225,7 +225,7 @@ class TableContentLayoutState extends State ? column.width + widget.scrollPadding.right : 0) <= widget.width) { - if (widget.shouldRenderColumnsLazy && centerOffset >= -column.width) { + if (!widget.shouldRenderColumnsLazy || centerOffset >= -column.width) { columnsCenter.add(i); columnOffsetsCenter.add(centerOffset); } From 215ee3df7e5bfef672be1713b7d80044e07910bc Mon Sep 17 00:00:00 2001 From: Yelzhan Yerkebulan <33483071+yerkejs@users.noreply.github.com> Date: Tue, 22 Jul 2025 11:07:27 +0500 Subject: [PATCH 3/3] Fix lazy issue on dynamic row heights --- lib/src/table_content_layout.dart | 77 +++++++++++++++++-------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/src/table_content_layout.dart b/lib/src/table_content_layout.dart index 5d5e2d4..a5550cc 100644 --- a/lib/src/table_content_layout.dart +++ b/lib/src/table_content_layout.dart @@ -219,44 +219,53 @@ class TableContentLayoutState extends State columnsLeft.add(i); columnOffsetsLeft.add(leftOffset); leftOffset += column.width; - } else if (leftOffset + - centerOffset + - (column.frozenAt(freezePriority) - ? column.width + widget.scrollPadding.right - : 0) <= - widget.width) { - if (!widget.shouldRenderColumnsLazy || centerOffset >= -column.width) { - columnsCenter.add(i); - columnOffsetsCenter.add(centerOffset); - } - centerOffset += column.width; } else { - sticky = true; - i = max(0, i - 2); - for (int j = columns.length - 1; j > i && j >= 0; j--) { - final column = columns[j]; - if (column.frozenAt(freezePriority)) { - if (column.sticky && sticky) { - _maxStickyHorizontalOffset += column.width; - } else { - sticky = false; - } - - columnsRight.add(j); - rightOffset -= column.width; - columnOffsetsRight.add(rightOffset); - - final maxVisibleOffset = widget.width - leftOffset + rightOffset; - while (columnsCenter.isNotEmpty && - columnOffsetsCenter.last > maxVisibleOffset) { - columnsCenter.removeLast(); - columnOffsetsCenter.removeLast(); - i--; + final columnFitsInVisibleArea = (leftOffset + + centerOffset + + (column.frozenAt(freezePriority) + ? column.width + widget.scrollPadding.right + : 0)) <= + widget.width; + + // when lazy rendering is disabled, we still want to include + // columns that don’t fit *provided* they are not frozen – frozen + // columns on the right must be handled by the dedicated branch + final shouldDisplayColumn = + (!widget.shouldRenderColumnsLazy && !column.frozenAt(freezePriority)); + + if (columnFitsInVisibleArea || shouldDisplayColumn) { + if (!widget.shouldRenderColumnsLazy || centerOffset >= -column.width) { + columnsCenter.add(i); + columnOffsetsCenter.add(centerOffset); + } + centerOffset += column.width; + } else { + sticky = true; + i = max(0, i - 2); + for (int j = columns.length - 1; j > i && j >= 0; j--) { + final column = columns[j]; + if (column.frozenAt(freezePriority)) { + if (column.sticky && sticky) { + _maxStickyHorizontalOffset += column.width; + } else { + sticky = false; + } + + columnsRight.add(j); + rightOffset -= column.width; + columnOffsetsRight.add(rightOffset); + + final maxVisibleOffset = widget.width - leftOffset + rightOffset; + while (columnsCenter.isNotEmpty && columnOffsetsCenter.last > maxVisibleOffset) { + columnsCenter.removeLast(); + columnOffsetsCenter.removeLast(); + i--; + } } } - } - break; + break; + } } }