-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5dd998
commit 838b216
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |