Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/flutter/lib/src/widgets/pinned_header_sliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';

import 'basic.dart';
import 'framework.dart';

/// A sliver that keeps its Widget child at the top of the a [CustomScrollView].
Expand Down Expand Up @@ -53,10 +54,26 @@ import 'framework.dart';
/// in response to downward and upwards scrolls.
/// * [SliverPersistentHeader] - a general purpose header that can be
/// configured as a pinned, resizing, or floating header.
class PinnedHeaderSliver extends SingleChildRenderObjectWidget {
class PinnedHeaderSliver extends StatelessWidget {
/// Creates a sliver whose [Widget] child appears at the top of a
/// [CustomScrollView].
const PinnedHeaderSliver({super.key, super.child});
const PinnedHeaderSliver({super.key, this.child});

/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final Widget? child;

@override
Widget build(BuildContext context) => _PinnedHeaderSliver(
child: Semantics(container: true, explicitChildNodes: true, child: child),
);
}

class _PinnedHeaderSliver extends SingleChildRenderObjectWidget {
/// Creates a sliver whose [Widget] child appears at the top of a
/// [CustomScrollView].
const _PinnedHeaderSliver({super.child});

@override
RenderObject createRenderObject(BuildContext context) {
Expand Down Expand Up @@ -106,4 +123,13 @@ class _RenderPinnedHeaderSliver extends RenderSliverSingleBoxAdapter {
hasVisualOverflow: true, // Conservatively say we do have overflow to avoid complexity.
);
}

@override
Comment thread
manu-sncf marked this conversation as resolved.
void describeSemanticsConfiguration(SemanticsConfiguration config) {
super.describeSemanticsConfiguration(config);

if (geometry != null && geometry!.layoutExtent < childExtent) {
config.addTagForChildren(RenderViewport.excludeFromScrolling);
}
}
}
71 changes: 71 additions & 0 deletions packages/flutter/test/widgets/pinned_header_sliver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';
import 'widgets_app_tester.dart';

void main() {
Expand Down Expand Up @@ -244,4 +246,73 @@ void main() {
expect(tester.getRect(find.text('PinnedHeaderSliver 1')), rect1);
expect(tester.getRect(find.text('PinnedHeaderSliver 2')), rect2);
});

// Regression test for https://github.com/flutter/flutter/issues/179022.
testWidgets(
'PinnedHeaderSliver: presence of RenderViewport.excludeFromScrolling tag when pinned',
(WidgetTester tester) async {
final semantics = SemanticsTester(tester);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would add the following to the top of this test.

// Regression test for https://github.com/flutter/flutter/issues/179022.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done.


await tester.pumpWidget(
TestWidgetsApp(
home: CustomScrollView(
slivers: <Widget>[
const SliverToBoxAdapter(child: SizedBox(height: 100, child: Text('First child'))),
const PinnedHeaderSliver(child: Text('PinnedHeaderSliver')),
SliverList.builder(
itemCount: 50,
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
),
],
),
),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should verify if the sliver actually gets pinned after the drag similar to other tests in this file.

Rect getHeaderRect() => tester.getRect(find.text('PinnedHeaderSliver'));
Rect getFirstChildRect() => tester.getRect(find.text('First child'));

// Pinned header appears after first child initially.
final Rect firstChildRect = getFirstChildRect();
expect(firstChildRect.top, 0.0);
expect(firstChildRect.height, 100.0);
expect(getHeaderRect().top, 100.0);

.....
....
...
..
. after drag....

// Pinned header should be pinned to the top after drag.
expect(getHeaderRect().top, 0.0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @Renzo-Olivares, I'm back from holidays 😄 ⛷️

Done.

Rect getHeaderRect() => tester.getRect(find.text('PinnedHeaderSliver'));
Rect getFirstChildRect() => tester.getRect(find.text('First child'));

// Pinned header appears after first child initially.
final Rect firstChildRect = getFirstChildRect();
expect(firstChildRect.top, 0.0);
expect(firstChildRect.height, 100.0);
expect(getHeaderRect().top, 100.0);

expect(
semantics,
isNot(
includesNodeWith(
tags: {RenderViewport.excludeFromScrolling, RenderViewport.useTwoPaneSemantics},
),
),
);

@Renzo-Olivares Renzo-Olivares Feb 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should verify that before the drag the sliver is not pinned and the exclude tag is not present and after the drag that it is pinned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Done.

await tester.drag(find.byType(CustomScrollView), const Offset(0, -100));
await tester.pumpAndSettle();

// Pinned header should be pinned to the top after drag.
expect(getHeaderRect().top, 0.0);

expect(
semantics,
isNot(
includesNodeWith(
tags: {RenderViewport.excludeFromScrolling, RenderViewport.useTwoPaneSemantics},
),
),
);

await tester.drag(find.byType(CustomScrollView), const Offset(0, -20));
await tester.pumpAndSettle();

final SemanticsNode? semanticNode = semantics
.nodesWith(label: 'PinnedHeaderSliver')
.firstOrNull;
expect(semanticNode?.parent?.tags, {
RenderViewport.excludeFromScrolling,
RenderViewport.useTwoPaneSemantics,
});

semantics.dispose();
},
);
}