Skip to content

Commit

Permalink
Added Sticky Header
Browse files Browse the repository at this point in the history
  • Loading branch information
EsinShadrach committed Jul 31, 2024
1 parent d5dd998 commit 838b216
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class HomePage extends StatelessWidget {
body: SafeArea(
child: ListView(
children: [
Text(routes().keys.toString()),
CupertinoListSection.insetGrouped(
backgroundColor:
context.colorScheme.inverseSurface.withOpacity(0.02),
Expand Down
3 changes: 3 additions & 0 deletions lib/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "package:flutter_playground/screens/examples/overlay_example/overlay_exam
import "package:flutter_playground/screens/examples/scale_down_btn/scale_down_btn_example.dart";
import "package:flutter_playground/screens/page_animation/page_animation.dart";
import "package:flutter_playground/screens/stacks/stacked_cards.dart";
import "package:flutter_playground/screens/sticky_header/sticky_header_screen.dart";
import "package:flutter_playground/screens/top_snackbar/top_snackbar.dart";

mixin AppRoutes {
Expand All @@ -17,6 +18,7 @@ mixin AppRoutes {
static const String heroWidget = "/hero-widget";
static const String topSnackbar = "/top-snackbar";
static const String stack = "/stack";
static const String stickerHeader = "/sticky-header";
}

Map<String, WidgetBuilder> routes() => {
Expand All @@ -28,4 +30,5 @@ Map<String, WidgetBuilder> routes() => {
AppRoutes.heroWidget: (context) => const HeroWidgetScreen(),
AppRoutes.topSnackbar: (context) => const TopSnackbar(),
AppRoutes.stack: (context) => const StackedCards(),
AppRoutes.stickerHeader: (context) => const StickyHeaderScreen(),
};
99 changes: 99 additions & 0 deletions lib/screens/sticky_header/sticky_header_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import "package:flutter/material.dart";

class StickyHeaderScreen extends StatelessWidget {
const StickyHeaderScreen({super.key});

@override
Widget build(BuildContext context) {
return const StickyHeaderExample();
}
}

class StickyHeaderExample extends StatelessWidget {
const StickyHeaderExample({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text("Item $index"),
);
},
childCount: 50, // Adjust this to change the number of list items
),
),
SliverPersistentHeader(
pinned: true,
floating: true,
delegate: _StickyHeaderDelegate(
minHeight: 60.0,
maxHeight: 60.0,
child: Container(
color: Colors.red,
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 30,
),
child: const SafeArea(
child: Center(
child: Text(
"Pinned Header",
style: TextStyle(color: Colors.white),
),
),
),
),
),
),
// This is the scrollable content
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text("Item $index"),
);
},
childCount: 50, // Adjust this to change the number of list items
),
),
],
),
);
}
}

class _StickyHeaderDelegate extends SliverPersistentHeaderDelegate {
final double minHeight;
final double maxHeight;
final Widget child;

_StickyHeaderDelegate({
required this.minHeight,
required this.maxHeight,
required this.child,
});

@override
double get minExtent => minHeight;

@override
double get maxExtent => maxHeight;

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

@override
bool shouldRebuild(_StickyHeaderDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}

0 comments on commit 838b216

Please sign in to comment.