Skip to content

Commit

Permalink
Use PinnedHeaderSliver instead of SliverPersistentHeader
Browse files Browse the repository at this point in the history
Using PinnedHeaderSliver removes the need for creating a
SliverPersistentHeaderDelegate and predicting the header's size.
  • Loading branch information
elibon99 committed Jan 10, 2025
1 parent ebf2689 commit bfa0741
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions lib/app/views/app_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -463,20 +463,15 @@ class _AppPageState extends ConsumerState<AppPage> {
slivers: [
SliverMainAxisGroup(
slivers: [
SliverPersistentHeader(
pinned: true,
delegate: _SliverTitleDelegate(
child: ColoredBox(
color: Theme.of(context).colorScheme.surface,
child: Padding(
key: _sliverTitleWrapperGlobalKey,
padding: const EdgeInsets.only(
left: 18.0, right: 18.0, bottom: 12.0, top: 4.0),
child: _buildTitle(context),
),
PinnedHeaderSliver(
child: ColoredBox(
color: Theme.of(context).colorScheme.surface,
child: Padding(
key: _sliverTitleWrapperGlobalKey,
padding: const EdgeInsets.only(
left: 18.0, right: 18.0, bottom: 12.0, top: 4.0),
child: _buildTitle(context),
),
// Header height = title height + vertical padding
height: _getTitleHeight(context) + 16,
),
),
if (widget.headerSliver != null)
Expand Down Expand Up @@ -1021,29 +1016,6 @@ class _VisibilityListenerState extends State<_VisibilityListener> {
}
}

class _SliverTitleDelegate extends SliverPersistentHeaderDelegate {
_SliverTitleDelegate({
required this.height,
required this.child,
});
final double height;
final Widget child;

@override
double get minExtent => height;
@override
double get maxExtent => height;

@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return child;
}

@override
bool shouldRebuild(_SliverTitleDelegate oldDelegate) => true;
}

class _NoImplicitScrollPhysics extends ScrollPhysics {
const _NoImplicitScrollPhysics({super.parent});

Expand Down

0 comments on commit bfa0741

Please sign in to comment.