Fix sticky section content overlap
This commit is contained in:
@@ -41,16 +41,15 @@ class StickySection extends StatelessWidget {
|
||||
);
|
||||
return SliverMainAxisGroup(
|
||||
slivers: [
|
||||
PinnedHeaderSliver(
|
||||
child: SizedBox(
|
||||
height: extent,
|
||||
child: StickySectionTitle(
|
||||
title: title,
|
||||
open: open,
|
||||
onToggle: onToggle,
|
||||
trailing: trailing,
|
||||
color: titleColor,
|
||||
),
|
||||
SliverPersistentHeader(
|
||||
pinned: true,
|
||||
delegate: _StickySectionHeaderDelegate(
|
||||
extent: extent,
|
||||
title: title,
|
||||
open: open,
|
||||
onToggle: onToggle,
|
||||
trailing: trailing,
|
||||
color: titleColor,
|
||||
),
|
||||
),
|
||||
// The content grows and shrinks under the pinned title, on its own
|
||||
@@ -72,6 +71,74 @@ class StickySection extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class _StickySectionHeaderDelegate extends SliverPersistentHeaderDelegate {
|
||||
_StickySectionHeaderDelegate({
|
||||
required this.extent,
|
||||
required this.title,
|
||||
required this.open,
|
||||
required this.onToggle,
|
||||
required this.trailing,
|
||||
required this.color,
|
||||
});
|
||||
|
||||
final double extent;
|
||||
final String title;
|
||||
final bool open;
|
||||
final VoidCallback onToggle;
|
||||
final List<Widget> trailing;
|
||||
final Color? color;
|
||||
|
||||
@override
|
||||
double get minExtent => extent;
|
||||
|
||||
@override
|
||||
double get maxExtent => extent;
|
||||
|
||||
@override
|
||||
Widget build(
|
||||
BuildContext context,
|
||||
double shrinkOffset,
|
||||
bool overlapsContent,
|
||||
) {
|
||||
final heading = SectionHeadingStyle.of(context);
|
||||
final inset = MediaQuery.paddingOf(context).top;
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
if (overlapsContent)
|
||||
Positioned(
|
||||
top: -inset,
|
||||
left: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
// The top-bar clearance is pinned ahead of this header. Once
|
||||
// this header is pinned, cover that clearance too so scrolled
|
||||
// content cannot remain visible above the section title.
|
||||
child: ColoredBox(color: heading.background),
|
||||
),
|
||||
StickySectionTitle(
|
||||
title: title,
|
||||
open: open,
|
||||
onToggle: onToggle,
|
||||
trailing: trailing,
|
||||
color: color,
|
||||
pinned: overlapsContent,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRebuild(_StickySectionHeaderDelegate oldDelegate) =>
|
||||
extent != oldDelegate.extent ||
|
||||
title != oldDelegate.title ||
|
||||
open != oldDelegate.open ||
|
||||
color != oldDelegate.color ||
|
||||
!identical(onToggle, oldDelegate.onToggle) ||
|
||||
!identical(trailing, oldDelegate.trailing);
|
||||
}
|
||||
|
||||
/// A sticky section's title bar: name, actions and the open/close chevron.
|
||||
class StickySectionTitle extends StatelessWidget {
|
||||
const StickySectionTitle({
|
||||
@@ -81,6 +148,7 @@ class StickySectionTitle extends StatelessWidget {
|
||||
required this.onToggle,
|
||||
this.trailing = const [],
|
||||
this.color,
|
||||
this.pinned = false,
|
||||
});
|
||||
|
||||
final String title;
|
||||
@@ -88,6 +156,7 @@ class StickySectionTitle extends StatelessWidget {
|
||||
final VoidCallback onToggle;
|
||||
final List<Widget> trailing;
|
||||
final Color? color;
|
||||
final bool pinned;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -140,9 +209,11 @@ class StickySectionTitle extends StatelessWidget {
|
||||
);
|
||||
// The whole strip opens and closes the section; its own buttons (copy)
|
||||
// still take their taps first.
|
||||
// The translucent page-colour scrim keeps this pinned heading readable as
|
||||
// content moves underneath without returning to the old solid section band.
|
||||
final scrim = heading.background.withValues(alpha: 0.94);
|
||||
// The page-colour scrim keeps this pinned heading readable as content moves
|
||||
// underneath without returning to the old solid section band.
|
||||
final scrim = pinned
|
||||
? heading.background
|
||||
: heading.background.withValues(alpha: 0.94);
|
||||
final Widget strip = cupertino
|
||||
? GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/adaptive/floating_bar_layout.dart';
|
||||
import 'package:pipelines_app/ui/layout/section_heading.dart';
|
||||
import 'package:pipelines_app/ui/layout/sticky_section.dart';
|
||||
|
||||
@@ -16,6 +18,7 @@ class _HostState extends State<_Host> {
|
||||
@override
|
||||
Widget build(BuildContext context) => CustomScrollView(
|
||||
slivers: [
|
||||
const SliverTopBarClearance(),
|
||||
StickySection(
|
||||
title: 'Input',
|
||||
open: open,
|
||||
@@ -48,6 +51,39 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
for (final host in ['Material', 'Cupertino']) {
|
||||
testWidgets('pins an opaque section edge below the top bar in $host', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(390, 600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final child = MediaQuery(
|
||||
data: const MediaQueryData(padding: EdgeInsets.only(top: 64)),
|
||||
child: _Host(),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
host == 'Cupertino'
|
||||
? CupertinoApp(home: child)
|
||||
: MaterialApp(home: Scaffold(body: child)),
|
||||
);
|
||||
|
||||
await tester.drag(find.byType(CustomScrollView), const Offset(0, -800));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final title = tester.widget<StickySectionTitle>(
|
||||
find.byType(StickySectionTitle),
|
||||
);
|
||||
expect(title.pinned, isTrue);
|
||||
expect(
|
||||
find.byWidgetPredicate(
|
||||
(widget) => widget is Positioned && widget.top == -64,
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('a tap anywhere on the title strip toggles the section', (
|
||||
tester,
|
||||
) async {
|
||||
|
||||
Reference in New Issue
Block a user