Skip to content

Commit 8fe7618

Browse files
authored
Fix the issue with pinned headers in nested SliverMainAxisGroup. (flutter#179132)
Fixes: flutter#178973 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent aaafeaf commit 8fe7618

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

packages/flutter/lib/src/rendering/sliver_group.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,22 @@ class RenderSliverMainAxisGroup extends RenderSliver
374374
// If the children's paint extent exceeds the remaining scroll extent of the `RenderSliverMainAxisGroup`,
375375
// they need to be corrected.
376376
if (paintOffset > remainingExtent) {
377+
// Whether the current remaining space can accommodate all pinned children.
378+
final bool pinnedChildrenOverflow =
379+
maxScrollObstructionExtent > remainingExtent - constraints.overlap;
377380
final double paintCorrection = paintOffset - remainingExtent;
378381
paintOffset = remainingExtent;
379382
child = firstChild;
380383
while (child != null) {
381384
final SliverGeometry childLayoutGeometry = child.geometry!;
382-
final bool childIsTooLarge = childLayoutGeometry.paintExtent > remainingExtent;
383-
final bool pinnedHeadersOverflow = maxScrollObstructionExtent > remainingExtent;
384-
final bool childIsPinnedHeader = childLayoutGeometry.maxScrollObstructionExtent > 0;
385-
if (childIsTooLarge || (pinnedHeadersOverflow && childIsPinnedHeader)) {
386-
final childParentData = child.parentData! as SliverPhysicalParentData;
385+
final childParentData = child.parentData! as SliverPhysicalParentData;
386+
final double childMainAxisPaintOffset = switch (constraints.axis) {
387+
Axis.vertical => childParentData.paintOffset.dy,
388+
Axis.horizontal => childParentData.paintOffset.dx,
389+
};
390+
final double childPaintEnd = childMainAxisPaintOffset + childLayoutGeometry.paintExtent;
391+
final bool childIsPinned = childLayoutGeometry.maxScrollObstructionExtent > 0;
392+
if (childPaintEnd > remainingExtent || (pinnedChildrenOverflow && childIsPinned)) {
387393
childParentData.paintOffset = switch (constraints.axis) {
388394
Axis.vertical => Offset(0.0, childParentData.paintOffset.dy - paintCorrection),
389395
Axis.horizontal => Offset(childParentData.paintOffset.dx - paintCorrection, 0.0),

packages/flutter/test/widgets/sliver_main_axis_group_test.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,69 @@ void main() {
15181518
expect(semantics.nodesWith(label: 'b'), hasLength(1));
15191519
semantics.dispose();
15201520
});
1521+
1522+
testWidgets(
1523+
'nested SliverMainAxisGroup with multiple PinnedHeaderSlivers positions correctly on scroll',
1524+
(WidgetTester tester) async {
1525+
final controller = ScrollController();
1526+
addTearDown(controller.dispose);
1527+
final List<Key> keys = [GlobalKey(), GlobalKey(), GlobalKey(), GlobalKey()];
1528+
1529+
Future<void> pumpWidget({bool reverse = false}) async {
1530+
return tester.pumpWidget(
1531+
_buildSliverMainAxisGroup(
1532+
controller: controller,
1533+
reverse: reverse,
1534+
viewportHeight: 300,
1535+
precedingSlivers: <Widget>[
1536+
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[0])),
1537+
],
1538+
otherSlivers: <Widget>[const SliverToBoxAdapter(child: SizedBox(height: 300))],
1539+
slivers: <Widget>[
1540+
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[1])),
1541+
SliverMainAxisGroup(
1542+
slivers: [
1543+
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[2])),
1544+
const SliverToBoxAdapter(child: SizedBox(height: 30)),
1545+
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[3])),
1546+
const SliverToBoxAdapter(child: SizedBox(height: 30)),
1547+
],
1548+
),
1549+
const SliverToBoxAdapter(child: SizedBox(height: 30)),
1550+
],
1551+
),
1552+
);
1553+
}
1554+
1555+
Future<void> verifyPositions(Map<double, List<double>> offsetToPositions) async {
1556+
for (final MapEntry<double, List<double>> entry in offsetToPositions.entries) {
1557+
controller.jumpTo(entry.key);
1558+
await tester.pumpAndSettle();
1559+
for (var i = 0; i < keys.length; i++) {
1560+
expect(tester.getTopLeft(find.byKey(keys[i])).dy, entry.value[i]);
1561+
}
1562+
}
1563+
}
1564+
1565+
// Forward direction
1566+
await pumpWidget();
1567+
await verifyPositions({
1568+
10: [0.0, 30.0, 60.0, 110.0],
1569+
40: [0.0, 30.0, 60.0, 90.0],
1570+
70: [0.0, 30.0, 50.0, 80.0],
1571+
100: [0.0, 30.0, 20.0, 50.0],
1572+
});
1573+
1574+
// Reverse direction
1575+
await pumpWidget(reverse: true);
1576+
await verifyPositions({
1577+
10: [270.0, 240.0, 210.0, 160.0],
1578+
40: [270.0, 240.0, 210.0, 180.0],
1579+
70: [270.0, 240.0, 220.0, 190.0],
1580+
100: [270.0, 240.0, 250.0, 220.0],
1581+
});
1582+
},
1583+
);
15211584
}
15221585

15231586
Widget _buildSliverList({
@@ -1559,6 +1622,7 @@ Widget _buildSliverMainAxisGroup({
15591622
Axis scrollDirection = Axis.vertical,
15601623
bool reverse = false,
15611624
List<Widget> otherSlivers = const <Widget>[],
1625+
List<Widget> precedingSlivers = const <Widget>[],
15621626
}) {
15631627
return MaterialApp(
15641628
home: Directionality(
@@ -1573,6 +1637,7 @@ Widget _buildSliverMainAxisGroup({
15731637
reverse: reverse,
15741638
controller: controller,
15751639
slivers: <Widget>[
1640+
...precedingSlivers,
15761641
SliverMainAxisGroup(slivers: slivers),
15771642
...otherSlivers,
15781643
],

0 commit comments

Comments
 (0)