Created
October 11, 2024 05:37
-
-
Save kumamotone/514ad9f723d69a8ee484346f266c067c to your computer and use it in GitHub Desktop.
DartPad Gist
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(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: CustomScrollView( | |
slivers: [ | |
// SliverAppBar | |
SliverAppBar( | |
expandedHeight: 200.0, | |
flexibleSpace: FlexibleSpaceBar( | |
title: Text('SliverAppBar'), | |
background: Image.network( | |
'https://placekitten.com/800/400', | |
fit: BoxFit.cover, | |
), | |
), | |
floating: false, | |
pinned: false, // | |
snap: false, | |
), | |
// | |
SliverPersistentHeader( | |
pinned: true, // | |
delegate: MySliverPersistentHeaderDelegate( | |
minExtent: 60.0, | |
maxExtent: 120.0, | |
), | |
), | |
// | |
SliverList( | |
delegate: SliverChildBuilderDelegate( | |
(BuildContext context, int index) { | |
return ListTile( | |
title: Text('Item #$index'), | |
); | |
}, | |
childCount: 50, | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
// | |
class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate { | |
final double minExtent; | |
final double maxExtent; | |
MySliverPersistentHeaderDelegate({ | |
required this.minExtent, | |
required this.maxExtent, | |
}); | |
@override | |
Widget build( | |
BuildContext context, double shrinkOffset, bool overlapsContent) { | |
return Container( | |
color: Colors.blue, | |
child: Center( | |
child: Text('Pinned Header', style: TextStyle(color: Colors.white)), | |
), | |
); | |
} | |
@override | |
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment