Fix the issue with pinned headers in nested SliverMainAxisGroup. (#179132)

Fixes: #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
This commit is contained in:
yim 2025-12-11 05:11:40 +08:00 committed by GitHub
parent aaafeaf18d
commit 8fe76181e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 5 deletions

View File

@ -374,16 +374,22 @@ class RenderSliverMainAxisGroup extends RenderSliver
// If the children's paint extent exceeds the remaining scroll extent of the `RenderSliverMainAxisGroup`,
// they need to be corrected.
if (paintOffset > remainingExtent) {
// Whether the current remaining space can accommodate all pinned children.
final bool pinnedChildrenOverflow =
maxScrollObstructionExtent > remainingExtent - constraints.overlap;
final double paintCorrection = paintOffset - remainingExtent;
paintOffset = remainingExtent;
child = firstChild;
while (child != null) {
final SliverGeometry childLayoutGeometry = child.geometry!;
final bool childIsTooLarge = childLayoutGeometry.paintExtent > remainingExtent;
final bool pinnedHeadersOverflow = maxScrollObstructionExtent > remainingExtent;
final bool childIsPinnedHeader = childLayoutGeometry.maxScrollObstructionExtent > 0;
if (childIsTooLarge || (pinnedHeadersOverflow && childIsPinnedHeader)) {
final childParentData = child.parentData! as SliverPhysicalParentData;
final childParentData = child.parentData! as SliverPhysicalParentData;
final double childMainAxisPaintOffset = switch (constraints.axis) {
Axis.vertical => childParentData.paintOffset.dy,
Axis.horizontal => childParentData.paintOffset.dx,
};
final double childPaintEnd = childMainAxisPaintOffset + childLayoutGeometry.paintExtent;
final bool childIsPinned = childLayoutGeometry.maxScrollObstructionExtent > 0;
if (childPaintEnd > remainingExtent || (pinnedChildrenOverflow && childIsPinned)) {
childParentData.paintOffset = switch (constraints.axis) {
Axis.vertical => Offset(0.0, childParentData.paintOffset.dy - paintCorrection),
Axis.horizontal => Offset(childParentData.paintOffset.dx - paintCorrection, 0.0),

View File

@ -1518,6 +1518,69 @@ void main() {
expect(semantics.nodesWith(label: 'b'), hasLength(1));
semantics.dispose();
});
testWidgets(
'nested SliverMainAxisGroup with multiple PinnedHeaderSlivers positions correctly on scroll',
(WidgetTester tester) async {
final controller = ScrollController();
addTearDown(controller.dispose);
final List<Key> keys = [GlobalKey(), GlobalKey(), GlobalKey(), GlobalKey()];
Future<void> pumpWidget({bool reverse = false}) async {
return tester.pumpWidget(
_buildSliverMainAxisGroup(
controller: controller,
reverse: reverse,
viewportHeight: 300,
precedingSlivers: <Widget>[
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[0])),
],
otherSlivers: <Widget>[const SliverToBoxAdapter(child: SizedBox(height: 300))],
slivers: <Widget>[
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[1])),
SliverMainAxisGroup(
slivers: [
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[2])),
const SliverToBoxAdapter(child: SizedBox(height: 30)),
PinnedHeaderSliver(child: SizedBox(height: 30, key: keys[3])),
const SliverToBoxAdapter(child: SizedBox(height: 30)),
],
),
const SliverToBoxAdapter(child: SizedBox(height: 30)),
],
),
);
}
Future<void> verifyPositions(Map<double, List<double>> offsetToPositions) async {
for (final MapEntry<double, List<double>> entry in offsetToPositions.entries) {
controller.jumpTo(entry.key);
await tester.pumpAndSettle();
for (var i = 0; i < keys.length; i++) {
expect(tester.getTopLeft(find.byKey(keys[i])).dy, entry.value[i]);
}
}
}
// Forward direction
await pumpWidget();
await verifyPositions({
10: [0.0, 30.0, 60.0, 110.0],
40: [0.0, 30.0, 60.0, 90.0],
70: [0.0, 30.0, 50.0, 80.0],
100: [0.0, 30.0, 20.0, 50.0],
});
// Reverse direction
await pumpWidget(reverse: true);
await verifyPositions({
10: [270.0, 240.0, 210.0, 160.0],
40: [270.0, 240.0, 210.0, 180.0],
70: [270.0, 240.0, 220.0, 190.0],
100: [270.0, 240.0, 250.0, 220.0],
});
},
);
}
Widget _buildSliverList({
@ -1559,6 +1622,7 @@ Widget _buildSliverMainAxisGroup({
Axis scrollDirection = Axis.vertical,
bool reverse = false,
List<Widget> otherSlivers = const <Widget>[],
List<Widget> precedingSlivers = const <Widget>[],
}) {
return MaterialApp(
home: Directionality(
@ -1573,6 +1637,7 @@ Widget _buildSliverMainAxisGroup({
reverse: reverse,
controller: controller,
slivers: <Widget>[
...precedingSlivers,
SliverMainAxisGroup(slivers: slivers),
...otherSlivers,
],