Skip to content

Instantly share code, notes, and snippets.

@shyjuzz
Created December 8, 2018 13:04
Show Gist options
  • Save shyjuzz/9cac508be0b6226ac7561f679b17d872 to your computer and use it in GitHub Desktop.
Save shyjuzz/9cac508be0b6226ac7561f679b17d872 to your computer and use it in GitHub Desktop.
class SilverAppBarWithTabBarScreen extends StatefulWidget {
@override
_SilverAppBarWithTabBarState createState() => _SilverAppBarWithTabBarState();
}
class _SilverAppBarWithTabBarState extends State<SilverAppBarWithTabBarScreen>
with SingleTickerProviderStateMixin {
TabController controller;
final items = List<String>.generate(100, (i) => "Item $i");
@override
void initState() {
super.initState();
controller = new TabController(length: 2, vsync: this);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new DefaultTabController(
length: 2,
child: new Scaffold(
body: new NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
new SliverAppBar(
pinned: true,
expandedHeight: 150.0,
forceElevated: innerBoxIsScrolled,
flexibleSpace: new FlexibleSpaceBar(
title: new Text('title'),
background: new Stack(
fit: StackFit.expand,
children: <Widget>[
new Image.network("http://placehold.it/640x320",
fit: BoxFit.cover, height: 256.0),
],
),
),
// bottom: new TabBar(
// tabs: [
// new Tab(text: 'tab1'),
// new Tab(text: 'tab2'),
// ],
// ),
),
SliverPersistentHeader(
delegate: _SliverAppBarDelegate(
TabBar(
labelColor: Colors.black87,
unselectedLabelColor: Colors.grey,
tabs: [
Tab(icon: Icon(Icons.info), text: "Tab 1"),
Tab(icon: Icon(Icons.lightbulb_outline), text: "Tab 2"),
],
),
),
pinned: true,
),
];
},
body: new TabBarView(
children: [
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index]}'),
);
},
),
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index]}'),
);
},
)
],
),
),
),
)
);
}
}
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate(this._tabBar);
final TabBar _tabBar;
@override
double get minExtent => _tabBar.preferredSize.height;
@override
double get maxExtent => _tabBar.preferredSize.height;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new Container(
child: _tabBar,
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment