Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/src/sliver_table_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -149,6 +150,7 @@ class _SliverTableViewState extends State<SliverTableView>
columns: columns,
scrollPadding: scrollPadding,
child: TableContentLayout(
shouldRenderColumnsLazy: widget.shouldRenderColumnsLazy,
verticalDividersStyle: style.dividers.vertical,
width: width,
fixedRowHeight: true,
Expand Down
79 changes: 45 additions & 34 deletions lib/src/table_content_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand Down Expand Up @@ -217,44 +219,53 @@ class TableContentLayoutState extends State<TableContentLayout>
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 (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;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/src/table_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<TableView> createState() => _TableViewState();
}
Expand Down Expand Up @@ -416,6 +424,7 @@ class _TableViewState extends State<TableView>
horizontalOffset: horizontalOffset,
stickyHorizontalOffset: _stickyHorizontalOffset,
minScrollableWidth: widget.minScrollableWidth,
shouldRenderColumnsLazy: widget.shouldRenderColumnsLazy,
child: Builder(
builder: (context) => TableScaffold(
shrinkWrapVertical: widget.shrinkWrapVertical,
Expand Down