Skip to content
Prev Previous commit
Next Next commit
++
  • Loading branch information
Piinks committed Mar 25, 2024
commit 3859c27db606200b7f70cd9ed4c42a158a417097
4 changes: 2 additions & 2 deletions packages/two_dimensional_scrollables/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class _TableExampleState extends State<TableExample> {
verticalDetails:
ScrollableDetails.vertical(controller: _verticalController),
cellBuilder: _buildCell,
columnCount: 20,
// columnCount: 20,
columnBuilder: _buildColumnSpan,
rowCount: _rowCount,
// rowCount: _rowCount,
rowBuilder: _buildRowSpan,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ class TableView extends TwoDimensionalScrollView {
super.clipBehavior,
int pinnedRowCount = 0,
int pinnedColumnCount = 0,
required int columnCount,
required int rowCount,
int? columnCount,
int? rowCount,
required TableSpanBuilder columnBuilder,
required TableSpanBuilder rowBuilder,
required TableViewCellBuilder cellBuilder,
}) : assert(pinnedRowCount >= 0),
assert(rowCount >= 0),
assert(rowCount >= pinnedRowCount),
assert(columnCount >= 0),
assert(rowCount == null || rowCount >= 0),
assert(rowCount == null || rowCount >= pinnedRowCount),
assert(columnCount == null || columnCount >= 0),
assert(pinnedColumnCount >= 0),
assert(columnCount >= pinnedColumnCount),
assert(columnCount == null || columnCount >= pinnedColumnCount),
super(
delegate: TableCellBuilderDelegate(
columnCount: columnCount,
Expand Down Expand Up @@ -412,7 +412,7 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
}

// Updates the cached column metrics for the table.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To help future readers: maybe document the meaning of the arguments to this method here since they are not immediately clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

void _updateColumnMetrics({int fromColumnIndex = 0, int? toColumnIndex,}) {
void _updateColumnMetrics({int fromColumnIndex = 0, int? toColumnIndex}) {
_firstNonPinnedColumn = null;
_lastNonPinnedColumn = null;
double startOfRegularColumn = 0;
Expand All @@ -424,11 +424,11 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
if (toColumnIndex != null) {
// Column metrics should be computed up to the provided index.
// Only relevant when we are filling in missing column metrics in an
//infinite context.
// infinite context.
assert(delegate.columnCount == null);
return newColumnMetrics.length > toColumnIndex;
} else if (delegate.columnCount == null) {
// There are infinite columns, and no target index, computing metrics
// There are infinite columns, and no target index, compute metrics
// up to what is visible and in the cache extent.
return _lastNonPinnedColumn != null;
}
Expand All @@ -454,9 +454,13 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
final double leadingOffset =
isPinned ? startOfPinnedColumn : startOfRegularColumn;
_Span? span = _columnMetrics.remove(column);
final TableSpan configuration = needsDelegateRebuild || span == null
final TableSpan? configuration = needsDelegateRebuild
? delegate.buildColumn(column)
: span.configuration;
: span?.configuration;
if (configuration == null) {
// We have reached the end of columns.
break;
}
span ??= _Span();
span.update(
isPinned: isPinned,
Expand Down Expand Up @@ -485,6 +489,7 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
}
column++;
}

assert(newColumnMetrics.length >= delegate.pinnedColumnCount);
for (final _Span span in _columnMetrics.values) {
span.dispose();
Expand All @@ -493,7 +498,7 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
}

// Updates the cached row metrics for the table.
void _updateRowMetrics({int fromRowIndex = 0, int? toRowIndex,}) {
void _updateRowMetrics({int fromRowIndex = 0, int? toRowIndex}) {
_firstNonPinnedRow = null;
_lastNonPinnedRow = null;
double startOfRegularRow = 0;
Expand All @@ -507,7 +512,7 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
_Span? span = _rowMetrics.remove(row);
assert(needsDelegateRebuild || span != null);
final TableSpan configuration =
needsDelegateRebuild ? delegate.buildRow(row) : span!.configuration;
needsDelegateRebuild ? delegate.buildRow(row) : span.configuration;
span ??= _Span();
span.update(
isPinned: isPinned,
Expand Down Expand Up @@ -878,6 +883,16 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
mergedColumnOffset = baseColumnOffset +
_columnMetrics[firstColumn]!.leadingOffset +
_columnMetrics[firstColumn]!.configuration.padding.leading;

if (delegate.columnCount == null && _columnMetrics[lastColumn] == null) {
// The number of columns is infinte, and we have not calculated
// the metrics to the full extent of the merged cell. Update the
// metrics so we have all the information for the merged area.
_updateColumnMetrics(
fromColumnIndex: _columnMetrics.length,
toColumnIndex: lastColumn,
);
}
mergedColumnWidth = _columnMetrics[lastColumn]!.trailingOffset -
_columnMetrics[firstColumn]!.leadingOffset -
_columnMetrics[lastColumn]!.configuration.padding.trailing -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import 'table_span.dart';
/// Used by the [TableCellDelegateMixin.buildColumn] and
/// [TableCellDelegateMixin.buildRow] to configure rows and columns in the
/// [TableView].
typedef TableSpanBuilder = TableSpan Function(int index);
typedef TableSpanBuilder = TableSpan? Function(int index);

/// Signature for a function that creates a child [TableViewCell] for a given
/// [TableVicinity] in a [TableView], but may return null.
///
/// Used by [TableCellBuilderDelegate.builder] to build cells on demand for the
/// table.
typedef TableViewCellBuilder = TableViewCell Function(
typedef TableViewCellBuilder = TableViewCell? Function(
BuildContext context,
TableVicinity vicinity,
);
Expand All @@ -45,6 +45,7 @@ mixin TableCellDelegateMixin on TwoDimensionalChildDelegate {
///
/// If the value returned by this getter changes throughout the lifetime of
/// the delegate object, [notifyListeners] must be called.
// TODO(Piinks): Update all docs where making nullable
int? get columnCount;

/// The number of rows that the table has content for.
Expand Down Expand Up @@ -102,13 +103,13 @@ mixin TableCellDelegateMixin on TwoDimensionalChildDelegate {
///
/// The builder must return a valid [TableSpan] for all indices smaller than
/// [columnCount].
TableSpan buildColumn(int index);
TableSpan? buildColumn(int index);

/// Builds the [TableSpan] that describe the row at the provided index.
///
/// The builder must return a valid [TableSpan] for all indices smaller than
/// [rowCount].
TableSpan buildRow(int index);
TableSpan? buildRow(int index);
}

/// A delegate that supplies children for a [TableViewport] on demand using a
Expand Down Expand Up @@ -169,7 +170,7 @@ class TableCellBuilderDelegate extends TwoDimensionalChildBuilderDelegate
/// [columnCount].
final TableSpanBuilder _columnBuilder;
@override
TableSpan buildColumn(int index) => _columnBuilder(index);
TableSpan? buildColumn(int index) => _columnBuilder(index);

@override
int get pinnedColumnCount => _pinnedColumnCount;
Expand Down Expand Up @@ -205,7 +206,7 @@ class TableCellBuilderDelegate extends TwoDimensionalChildBuilderDelegate
/// [rowCount].
final TableSpanBuilder _rowBuilder;
@override
TableSpan buildRow(int index) => _rowBuilder(index);
TableSpan? buildRow(int index) => _rowBuilder(index);

@override
int get pinnedRowCount => _pinnedRowCount;
Expand Down Expand Up @@ -274,7 +275,13 @@ class TableCellListDelegate extends TwoDimensionalChildListDelegate
/// [columnCount].
final TableSpanBuilder _columnBuilder;
@override
TableSpan buildColumn(int index) => _columnBuilder(index);
TableSpan? buildColumn(int index) {
if (index >= columnCount) {
// The list delegate has a finite number of columns.
return null;
}
return _columnBuilder(index);
}

@override
int get pinnedColumnCount => _pinnedColumnCount;
Expand All @@ -298,7 +305,13 @@ class TableCellListDelegate extends TwoDimensionalChildListDelegate
/// [rowCount].
final TableSpanBuilder _rowBuilder;
@override
TableSpan buildRow(int index) => _rowBuilder(index);
TableSpan? buildRow(int index) {
if (index >= rowCount) {
// The list deleagte has a finite number of rows.
return null;
}
return _rowBuilder(index);
}

@override
int get pinnedRowCount => _pinnedRowCount;
Expand Down