Skip to content

Commit 2826fb1

Browse files
committed
[two_dimensional_scrollables] Search only regular spans for the visible range
_binarySearchFirstFromMap searched the whole metrics map, but trailing pinned spans sit at its end and fail the regular-span condition, so the condition is not monotonic across the map. Whenever the search probed a trailing pinned span it discarded the regular spans before it: with columnCount: 3 and trailingPinnedColumnCount: 2, a scroll left no regular column in the visible range and its cells disappeared. Restrict the search to the indices of the regular spans, and cover more than one trailing pinned row and column.
1 parent aedd88c commit 2826fb1

3 files changed

Lines changed: 103 additions & 3 deletions

File tree

‎packages/two_dimensional_scrollables/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.5.5
22

33
* Fixes `TableView` laying out and painting trailing pinned rows and columns twice, which could throw when the table was scrolled to the end.
4+
* Fixes `TableView` losing its regular rows or columns on scroll when more than one row or column is trailing pinned.
45

56
## 0.5.4
67

‎packages/two_dimensional_scrollables/lib/src/table_view/table.dart‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,12 +1036,23 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
10361036
/// Binary search to find the first index with [_Span] matching the condition.
10371037
/// [map]: Index-[_Span] map, [condition]: Match rule
10381038
/// Returns the first matched index or null if not found.
1039-
int? _binarySearchFirstFromMap(Map<int, _Span> map, bool Function(_Span) condition) {
1039+
///
1040+
/// Only indices from [first] to [last] are searched. [condition] must be
1041+
/// false for every index before the first match and true for every index
1042+
/// after it within that range, so callers searching for regular spans pass
1043+
/// the range of regular spans: the pinned spans on either side of it would
1044+
/// break that ordering.
1045+
int? _binarySearchFirstFromMap(
1046+
Map<int, _Span> map,
1047+
bool Function(_Span) condition, {
1048+
int first = 0,
1049+
int? last,
1050+
}) {
10401051
if (map.isEmpty) {
10411052
return null;
10421053
}
1043-
var low = 0;
1044-
int high = map.length - 1;
1054+
var low = first;
1055+
int high = math.min(last ?? map.length - 1, map.length - 1);
10451056
int? result;
10461057
while (low <= high) {
10471058
final int mid = low + ((high - low) >> 1);
@@ -1081,10 +1092,14 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
10811092
_firstNonPinnedColumn = _binarySearchFirstFromMap(
10821093
_columnMetrics,
10831094
(span) => !span.isPinned && span.trailingOffset >= _targetLeadingColumnPixel,
1095+
first: delegate.pinnedColumnCount,
1096+
last: _lastRegularColumnIndex,
10841097
);
10851098
_lastNonPinnedColumn = _binarySearchFirstFromMap(
10861099
_columnMetrics,
10871100
(span) => !span.isPinned && span.trailingOffset >= _targetTrailingColumnPixel,
1101+
first: delegate.pinnedColumnCount,
1102+
last: _lastRegularColumnIndex,
10881103
);
10891104
if (_firstNonPinnedColumn != null) {
10901105
// Trailing pinned columns are laid out and painted separately, so the
@@ -1112,10 +1127,14 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
11121127
_firstNonPinnedRow = _binarySearchFirstFromMap(
11131128
_rowMetrics,
11141129
(span) => !span.isPinned && span.trailingOffset >= _targetLeadingRowPixel,
1130+
first: delegate.pinnedRowCount,
1131+
last: _lastRegularRowIndex,
11151132
);
11161133
_lastNonPinnedRow = _binarySearchFirstFromMap(
11171134
_rowMetrics,
11181135
(span) => !span.isPinned && span.trailingOffset >= _targetTrailingRowPixel,
1136+
first: delegate.pinnedRowCount,
1137+
last: _lastRegularRowIndex,
11191138
);
11201139
if (_firstNonPinnedRow != null) {
11211140
// Trailing pinned rows are laid out and painted separately, so the range

‎packages/two_dimensional_scrollables/test/table_view/table_test.dart‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,6 +4183,86 @@ void main() {
41834183
expect(find.text('R18 C19'), findsOneWidget);
41844184
});
41854185

4186+
testWidgets('Regular columns are found when more than one column is trailing pinned', (
4187+
WidgetTester tester,
4188+
) async {
4189+
final verticalController = ScrollController();
4190+
addTearDown(verticalController.dispose);
4191+
4192+
await tester.pumpWidget(
4193+
MaterialApp(
4194+
home: Scaffold(
4195+
body: SizedBox(
4196+
height: 400,
4197+
width: 400,
4198+
child: TableView.builder(
4199+
columnCount: 3,
4200+
rowCount: 20,
4201+
trailingPinnedColumnCount: 2,
4202+
verticalDetails: ScrollableDetails.vertical(controller: verticalController),
4203+
columnBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4204+
rowBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4205+
cellBuilder: (BuildContext context, TableVicinity vicinity) {
4206+
return TableViewCell(child: Text('R${vicinity.row} C${vicinity.column}'));
4207+
},
4208+
),
4209+
),
4210+
),
4211+
),
4212+
);
4213+
expect(find.text('R0 C0'), findsOneWidget);
4214+
4215+
// Scrolling lays the table out again from the cached metrics, which binary
4216+
// searches them for the first and last regular column. The trailing pinned
4217+
// columns at the end of the metrics must not steer that search away from
4218+
// the only regular column.
4219+
verticalController.jumpTo(100);
4220+
await tester.pump();
4221+
4222+
expect(tester.takeException(), isNull);
4223+
expect(find.text('R1 C0'), findsOneWidget);
4224+
expect(find.text('R1 C1'), findsOneWidget);
4225+
expect(find.text('R1 C2'), findsOneWidget);
4226+
});
4227+
4228+
testWidgets('Regular rows are found when more than one row is trailing pinned', (
4229+
WidgetTester tester,
4230+
) async {
4231+
final horizontalController = ScrollController();
4232+
addTearDown(horizontalController.dispose);
4233+
4234+
await tester.pumpWidget(
4235+
MaterialApp(
4236+
home: Scaffold(
4237+
body: SizedBox(
4238+
height: 400,
4239+
width: 400,
4240+
child: TableView.builder(
4241+
columnCount: 20,
4242+
rowCount: 3,
4243+
trailingPinnedRowCount: 2,
4244+
horizontalDetails: ScrollableDetails.horizontal(controller: horizontalController),
4245+
columnBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4246+
rowBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4247+
cellBuilder: (BuildContext context, TableVicinity vicinity) {
4248+
return TableViewCell(child: Text('R${vicinity.row} C${vicinity.column}'));
4249+
},
4250+
),
4251+
),
4252+
),
4253+
),
4254+
);
4255+
expect(find.text('R0 C0'), findsOneWidget);
4256+
4257+
horizontalController.jumpTo(100);
4258+
await tester.pump();
4259+
4260+
expect(tester.takeException(), isNull);
4261+
expect(find.text('R0 C1'), findsOneWidget);
4262+
expect(find.text('R1 C1'), findsOneWidget);
4263+
expect(find.text('R2 C1'), findsOneWidget);
4264+
});
4265+
41864266
testWidgets('Intersections of leading and trailing pinned', (WidgetTester tester) async {
41874267
const span = TableSpan(extent: FixedTableSpanExtent(100));
41884268
await tester.pumpWidget(

0 commit comments

Comments
 (0)