Created
October 23, 2025 14:23
-
-
Save kekland/0e20282864fa1ba0f08bbfa46aaa4e4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(const App()); | |
| } | |
| class App extends StatelessWidget { | |
| const App({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp(home: DemoPageWithMainAxisGroup()); | |
| // return MaterialApp(home: DemoPageWithoutMainAxisGroup()); | |
| } | |
| } | |
| class DemoPageWithMainAxisGroup extends StatelessWidget { | |
| const DemoPageWithMainAxisGroup({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: SafeArea( | |
| bottom: false, | |
| child: CustomScrollView( | |
| slivers: [ | |
| SliverMainAxisGroup( | |
| slivers: [ | |
| SliverPersistentHeader( | |
| pinned: true, | |
| delegate: MySliverPersistentHeaderDelegate(), | |
| ), | |
| MyItemList(), | |
| ], | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class DemoPageWithoutMainAxisGroup extends StatelessWidget { | |
| const DemoPageWithoutMainAxisGroup({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: SafeArea( | |
| bottom: false, | |
| child: CustomScrollView( | |
| slivers: [ | |
| SliverPersistentHeader( | |
| pinned: true, | |
| delegate: MySliverPersistentHeaderDelegate(), | |
| ), | |
| MyItemList(), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate { | |
| @override | |
| Widget build( | |
| BuildContext context, | |
| double shrinkOffset, | |
| bool overlapsContent, | |
| ) { | |
| return Container( | |
| color: Colors.orange, | |
| alignment: Alignment.center, | |
| child: Padding( | |
| padding: const EdgeInsets.symmetric(horizontal: 20.0), | |
| child: TextField( | |
| decoration: InputDecoration(hintText: 'Search...'), | |
| onTapUpOutside: (_) => FocusScope.of(context).unfocus(), | |
| ), | |
| ), | |
| ); | |
| } | |
| @override | |
| double get maxExtent => 64.0; | |
| @override | |
| double get minExtent => 64.0; | |
| @override | |
| bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) => | |
| false; | |
| } | |
| class MyItemList extends StatelessWidget { | |
| const MyItemList({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return SliverList( | |
| delegate: SliverChildBuilderDelegate( | |
| (context, index) => ListTile( | |
| title: Text('Item #$index'), | |
| tileColor: Colors.primaries[index % Colors.primaries.length], | |
| ), | |
| childCount: 50, | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment