Skip to content

Instantly share code, notes, and snippets.

@mosluce
Created August 23, 2022 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosluce/a10817ed356cdb16f305c1c7f327b118 to your computer and use it in GitHub Desktop.
Save mosluce/a10817ed356cdb16f305c1c7f327b118 to your computer and use it in GitHub Desktop.
MySliverPersistentHeader example
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({super.key, required this.title});
@override
State<StatefulWidget> createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.grey.shade50,
child: Column(
children: [
buildAppBar(context),
Expanded(
child: DefaultTabController(
length: 2,
child: CustomScrollView(
slivers: [
buildHeader(context),
buildList(context),
],
),
),
),
],
),
),
);
}
buildAppBar(BuildContext context) {
return Container(
height: 100,
color: const Color.fromARGB(255, 0, 49, 90),
);
}
buildAvatar(BuildContext context) {
return Container(
width: 180,
height: 180,
margin: const EdgeInsets.symmetric(vertical: 16),
color: Colors.blueGrey,
child: const Center(
child: Text("Avatar"),
),
);
}
buildDisplayName(BuildContext context) {
return Center(
child: Container(
width: 120,
height: 30,
color: Colors.blueGrey,
),
);
}
buildTabBar(BuildContext context) {
return Container(
height: 60,
color: Colors.grey,
margin: const EdgeInsets.only(top: 8, left: 24, right: 24, bottom: 0),
);
}
buildHeader(BuildContext context) {
return SliverPersistentHeader(
pinned: true,
delegate: MySliverPersistentHeaderDelegate(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
buildAvatar(context),
buildDisplayName(context),
buildTabBar(context),
],
),
// child: buildAvatar(context),
),
);
}
buildList(BuildContext context) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
padding: const EdgeInsets.all(8),
alignment: Alignment.centerLeft,
height: 40,
child: Text('$index'),
);
},
childCount: 100,
),
);
}
}
class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
final Widget child;
MySliverPersistentHeaderDelegate({required this.child});
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
print(shrinkOffset);
// print(maxExtent - shrinkOffset);
return Stack(
children: [
Positioned.fill(
top: 0 - shrinkOffset,
child: Container(
color: Colors.orange,
child: Align(
alignment: Alignment.bottomCenter,
child: child,
),
),
)
],
);
}
@override
double get maxExtent => 310;
@override
double get minExtent => 60;
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment