Skip to content

Instantly share code, notes, and snippets.

@kumamotone
Created October 11, 2024 05:37
Show Gist options
  • Save kumamotone/514ad9f723d69a8ee484346f266c067c to your computer and use it in GitHub Desktop.
Save kumamotone/514ad9f723d69a8ee484346f266c067c to your computer and use it in GitHub Desktop.
DartPad Gist
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