Skip to content

Commit c983254

Browse files
authored
[two_dimensional_scrollables] Exclude trailing pinned spans from the non-pinned range (#12666)
`RenderTableViewport` lays out and paints the table as nine regions: leading pinned, regular and trailing pinned rows, each crossed with the same three column categories. `_updateFirstAndLastVisibleCell` binary searches for the last regular row and column of the visible range, and when no regular span reaches the trailing edge of the layout target it fell back to the last index in the metrics map: ```dart _lastNonPinnedColumn ??= _columnMetrics.length - 1; ``` Whenever `trailingPinnedRowCount` or `trailingPinnedColumnCount` is greater than zero, that last index *is* a trailing pinned span. The regular range then overlaps the trailing pinned range, and the same `TableVicinity` is visited by two regions in a single layout and paint pass. `_updateColumnMetrics` and `_updateRowMetrics` already cap the same value correctly, with the rule that `_lastRegularColumnIndex` and `_lastRegularRowIndex` express, so the two code paths disagreed. This PR makes `_updateFirstAndLastVisibleCell` use those getters. The old fallback is kept for the case where they are null — infinite spans with no null terminator — where trailing pinned spans cannot exist, since `_firstTrailingPinnedColumn` and `_firstTrailingPinnedRow` both require a non-null span count. Fixes flutter/flutter#185842 Fixes flutter/flutter#190800 ## Beyond the duplicated work The overlap is not only wasted layout. Scrolling a table to the end with a trailing pinned span throws, because two regions claim the same cell: ``` Expected to re-use an element at (row: 0, column: 19), but none was found. package:flutter/src/widgets/two_dimensional_viewport.dart:377 'elementToReuse != null' ``` and, once the spans carry a decoration, the non-pinned region reaches a cell whose `paintOffset` was never set for it: ``` Null check operator used on a null value RenderTableViewport._paintCells.getColumnRect (table.dart:1888) ``` Dragging through a 60x60 table with pinned and trailing pinned rows and columns throws `Expected to re-use an element at ...` and `TableViewCell for (row: 1, column: 58) could not be found` before this change, and nothing after it. That is the same subsystem and trigger as flutter/flutter#190800, but the specific assertion reported there did not reproduce in my runs, so I am not claiming this fixes it. ## Tests Two regression tests are added to `table_test.dart`, one per axis. Each scrolls a table with a trailing pinned span to the end, asserts that no exception is thrown, and asserts that the trailing pinned span's decoration is painted exactly once — by the trailing pinned region only. Both fail before this change. Counting `cellBuilder` calls does not work as a test here: `buildOrObtainChildFor` caches children for the frame and `RenderObject.layout` early-returns on unchanged constraints, so the duplicated visit is invisible from the delegate. The new `CountingSpanDecoration` helper observes it through the public `TableSpanDecoration.paint` API instead. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 6b8b927 commit c983254

4 files changed

Lines changed: 291 additions & 10 deletions

File tree

‎packages/two_dimensional_scrollables/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 0.5.5
22

3+
* 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.
35
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
46

57
## 0.5.4

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,27 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
10331033
return verticalOffset.applyContentDimensions(0.0, maxVerticalScrollExtent);
10341034
}
10351035

1036-
/// Binary search to find the first index with [_Span] matching the condition.
1037-
/// [map]: Index-[_Span] map, [condition]: Match rule
1038-
/// Returns the first matched index or null if not found.
1039-
int? _binarySearchFirstFromMap(Map<int, _Span> map, bool Function(_Span) condition) {
1036+
/// Returns the first index in the given [map] whose [_Span] satisfies the
1037+
/// [condition], searching only from [first] to [last].
1038+
///
1039+
/// If no [_Span] in that range satisfies the [condition], this will return
1040+
/// null.
1041+
///
1042+
/// This is a binary search, so once the [condition] is true for an index, it
1043+
/// must remain true for every following index in the range. Pinned spans do
1044+
/// not follow this order, so callers should limit the range to the regular
1045+
/// spans.
1046+
int? _binarySearchFirstFromMap(
1047+
Map<int, _Span> map,
1048+
bool Function(_Span) condition, {
1049+
int first = 0,
1050+
int? last,
1051+
}) {
10401052
if (map.isEmpty) {
10411053
return null;
10421054
}
1043-
var low = 0;
1044-
int high = map.length - 1;
1055+
var low = first;
1056+
int high = math.min(last ?? map.length - 1, map.length - 1);
10451057
int? result;
10461058
while (low <= high) {
10471059
final int mid = low + ((high - low) >> 1);
@@ -1081,13 +1093,19 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
10811093
_firstNonPinnedColumn = _binarySearchFirstFromMap(
10821094
_columnMetrics,
10831095
(span) => !span.isPinned && span.trailingOffset >= _targetLeadingColumnPixel,
1096+
first: delegate.pinnedColumnCount,
1097+
last: _lastRegularColumnIndex,
10841098
);
10851099
_lastNonPinnedColumn = _binarySearchFirstFromMap(
10861100
_columnMetrics,
10871101
(span) => !span.isPinned && span.trailingOffset >= _targetTrailingColumnPixel,
1102+
first: delegate.pinnedColumnCount,
1103+
last: _lastRegularColumnIndex,
10881104
);
10891105
if (_firstNonPinnedColumn != null) {
1090-
_lastNonPinnedColumn ??= _columnMetrics.length - 1;
1106+
// Trailing pinned columns are laid out and painted separately, so the
1107+
// range of regular columns must never extend into them.
1108+
_lastNonPinnedColumn ??= _lastRegularColumnIndex ?? _columnMetrics.length - 1;
10911109
}
10921110

10931111
if (_rowMetrics.isNotEmpty) {
@@ -1110,13 +1128,19 @@ class RenderTableViewport extends RenderTwoDimensionalViewport {
11101128
_firstNonPinnedRow = _binarySearchFirstFromMap(
11111129
_rowMetrics,
11121130
(span) => !span.isPinned && span.trailingOffset >= _targetLeadingRowPixel,
1131+
first: delegate.pinnedRowCount,
1132+
last: _lastRegularRowIndex,
11131133
);
11141134
_lastNonPinnedRow = _binarySearchFirstFromMap(
11151135
_rowMetrics,
11161136
(span) => !span.isPinned && span.trailingOffset >= _targetTrailingRowPixel,
1137+
first: delegate.pinnedRowCount,
1138+
last: _lastRegularRowIndex,
11171139
);
11181140
if (_firstNonPinnedRow != null) {
1119-
_lastNonPinnedRow ??= _rowMetrics.length - 1;
1141+
// Trailing pinned rows are laid out and painted separately, so the range
1142+
// of regular rows must never extend into them.
1143+
_lastNonPinnedRow ??= _lastRegularRowIndex ?? _rowMetrics.length - 1;
11201144
}
11211145
}
11221146

‎packages/two_dimensional_scrollables/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: two_dimensional_scrollables
22
description: Widgets that scroll using the two dimensional scrolling foundation.
3-
version: 0.5.4
3+
version: 0.5.5
44
repository: https://github.com/flutter/packages/tree/main/packages/two_dimensional_scrollables
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+two_dimensional_scrollables%22+
66

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

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ TableSpan getMouseTrackingSpan(
3939
);
4040
}
4141

42+
// Counts how many times a span's decoration is painted, keyed by span index.
43+
class CountingSpanDecoration extends TableSpanDecoration {
44+
const CountingSpanDecoration({required this.index, required this.paintCounts});
45+
46+
final int index;
47+
final Map<int, int> paintCounts;
48+
49+
@override
50+
void paint(TableSpanDecorationPaintDetails details) {
51+
paintCounts.update(index, (int count) => count + 1, ifAbsent: () => 1);
52+
super.paint(details);
53+
}
54+
}
55+
4256
void main() {
4357
group('TableView.builder', () {
4458
testWidgets('creates correct delegate', (WidgetTester tester) async {
@@ -4008,6 +4022,247 @@ void main() {
40084022
expect(tester.getRect(find.text('R9 C9')).top, 300);
40094023
});
40104024

4025+
// Regression test for https://github.com/flutter/flutter/issues/185842.
4026+
testWidgets('Trailing pinned columns are excluded from the non-pinned column range', (
4027+
WidgetTester tester,
4028+
) async {
4029+
final horizontalController = ScrollController();
4030+
addTearDown(horizontalController.dispose);
4031+
final paintCounts = <int, int>{};
4032+
4033+
await tester.pumpWidget(
4034+
MaterialApp(
4035+
home: Scaffold(
4036+
body: SizedBox(
4037+
height: 400,
4038+
width: 400,
4039+
child: TableView.builder(
4040+
columnCount: 20,
4041+
rowCount: 4,
4042+
trailingPinnedColumnCount: 1,
4043+
horizontalDetails: ScrollableDetails.horizontal(controller: horizontalController),
4044+
columnBuilder: (int index) => TableSpan(
4045+
extent: const FixedTableSpanExtent(100),
4046+
backgroundDecoration: CountingSpanDecoration(
4047+
index: index,
4048+
paintCounts: paintCounts,
4049+
),
4050+
),
4051+
rowBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4052+
cellBuilder: (BuildContext context, TableVicinity vicinity) {
4053+
return TableViewCell(child: Text('R${vicinity.row} C${vicinity.column}'));
4054+
},
4055+
),
4056+
),
4057+
),
4058+
),
4059+
);
4060+
4061+
// Once scrolled to the end, no regular column reaches the trailing edge of
4062+
// the layout target. That is where the range of non-pinned columns used to
4063+
// fall back to the last column of the table, which is a trailing pinned
4064+
// one, making it part of both the non-pinned and the trailing pinned
4065+
// region.
4066+
paintCounts.clear();
4067+
horizontalController.jumpTo(horizontalController.position.maxScrollExtent);
4068+
await tester.pump();
4069+
4070+
expect(tester.takeException(), isNull);
4071+
// The trailing pinned column is decorated once, by the trailing pinned
4072+
// region only.
4073+
expect(paintCounts[19], 1);
4074+
expect(find.text('R0 C19'), findsOneWidget);
4075+
});
4076+
4077+
// Regression test for https://github.com/flutter/flutter/issues/185842.
4078+
testWidgets('Trailing pinned rows are excluded from the non-pinned row range', (
4079+
WidgetTester tester,
4080+
) async {
4081+
final verticalController = ScrollController();
4082+
addTearDown(verticalController.dispose);
4083+
final paintCounts = <int, int>{};
4084+
4085+
await tester.pumpWidget(
4086+
MaterialApp(
4087+
home: Scaffold(
4088+
body: SizedBox(
4089+
height: 400,
4090+
width: 400,
4091+
child: TableView.builder(
4092+
columnCount: 4,
4093+
rowCount: 20,
4094+
trailingPinnedRowCount: 1,
4095+
verticalDetails: ScrollableDetails.vertical(controller: verticalController),
4096+
columnBuilder: (int index) => const TableSpan(extent: FixedTableSpanExtent(100)),
4097+
rowBuilder: (int index) => TableSpan(
4098+
extent: const FixedTableSpanExtent(100),
4099+
backgroundDecoration: CountingSpanDecoration(
4100+
index: index,
4101+
paintCounts: paintCounts,
4102+
),
4103+
),
4104+
cellBuilder: (BuildContext context, TableVicinity vicinity) {
4105+
return TableViewCell(child: Text('R${vicinity.row} C${vicinity.column}'));
4106+
},
4107+
),
4108+
),
4109+
),
4110+
),
4111+
);
4112+
4113+
paintCounts.clear();
4114+
verticalController.jumpTo(verticalController.position.maxScrollExtent);
4115+
await tester.pump();
4116+
4117+
expect(tester.takeException(), isNull);
4118+
expect(paintCounts[19], 1);
4119+
expect(find.text('R19 C0'), findsOneWidget);
4120+
});
4121+
4122+
// Regression test for https://github.com/flutter/flutter/issues/185842.
4123+
testWidgets('Trailing pinned rows and columns are excluded from the non-pinned ranges together', (
4124+
WidgetTester tester,
4125+
) async {
4126+
final verticalController = ScrollController();
4127+
addTearDown(verticalController.dispose);
4128+
final horizontalController = ScrollController();
4129+
addTearDown(horizontalController.dispose);
4130+
final rowPaintCounts = <int, int>{};
4131+
final columnPaintCounts = <int, int>{};
4132+
4133+
await tester.pumpWidget(
4134+
MaterialApp(
4135+
home: Scaffold(
4136+
body: SizedBox(
4137+
height: 400,
4138+
width: 400,
4139+
child: TableView.builder(
4140+
columnCount: 20,
4141+
rowCount: 20,
4142+
trailingPinnedColumnCount: 1,
4143+
trailingPinnedRowCount: 1,
4144+
verticalDetails: ScrollableDetails.vertical(controller: verticalController),
4145+
horizontalDetails: ScrollableDetails.horizontal(controller: horizontalController),
4146+
columnBuilder: (int index) => TableSpan(
4147+
extent: const FixedTableSpanExtent(100),
4148+
backgroundDecoration: CountingSpanDecoration(
4149+
index: index,
4150+
paintCounts: columnPaintCounts,
4151+
),
4152+
),
4153+
rowBuilder: (int index) => TableSpan(
4154+
extent: const FixedTableSpanExtent(100),
4155+
backgroundDecoration: CountingSpanDecoration(
4156+
index: index,
4157+
paintCounts: rowPaintCounts,
4158+
),
4159+
),
4160+
cellBuilder: (BuildContext context, TableVicinity vicinity) {
4161+
return TableViewCell(child: Text('R${vicinity.row} C${vicinity.column}'));
4162+
},
4163+
),
4164+
),
4165+
),
4166+
),
4167+
);
4168+
4169+
rowPaintCounts.clear();
4170+
columnPaintCounts.clear();
4171+
verticalController.jumpTo(verticalController.position.maxScrollExtent);
4172+
horizontalController.jumpTo(horizontalController.position.maxScrollExtent);
4173+
await tester.pump();
4174+
4175+
expect(tester.takeException(), isNull);
4176+
// Each trailing pinned span is decorated once for each region it shares
4177+
// with the other axis: the regular region and the trailing pinned one.
4178+
expect(rowPaintCounts[19], 2);
4179+
expect(columnPaintCounts[19], 2);
4180+
// The corner cell belongs to the trailing pinned row and column only.
4181+
expect(find.text('R19 C19'), findsOneWidget);
4182+
expect(find.text('R19 C18'), findsOneWidget);
4183+
expect(find.text('R18 C19'), findsOneWidget);
4184+
});
4185+
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+
40114266
testWidgets('Intersections of leading and trailing pinned', (WidgetTester tester) async {
40124267
const span = TableSpan(extent: FixedTableSpanExtent(100));
40134268
await tester.pumpWidget(

0 commit comments

Comments
 (0)