Created
January 15, 2022 18:50
-
-
Save TheGlorySaint/3e0398841095822b768b97b8694639f7 to your computer and use it in GitHub Desktop.
SliverAppBar with title and content and when scrolled content is not displayed
This file contains 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( | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MainCollapsingToolbar(), | |
); | |
} | |
} | |
class MainCollapsingToolbar extends StatefulWidget { | |
@override | |
_MainCollapsingToolbarState createState() => _MainCollapsingToolbarState(); | |
} | |
class _MainCollapsingToolbarState extends State<MainCollapsingToolbar> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: DefaultTabController( | |
length: 2, | |
child: NestedScrollView( | |
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { | |
return <Widget>[ | |
SliverAppBar( | |
expandedHeight: 200.0, | |
floating: false, | |
pinned: true, | |
flexibleSpace: FlexibleSpaceBar( | |
centerTitle: true, | |
title: const Text( | |
"Collapsing Toolbar", | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 16.0, | |
), | |
), | |
background: Image.network( | |
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350", | |
fit: BoxFit.cover, | |
), | |
), | |
), | |
SliverPersistentHeader( | |
floating: true, | |
delegate: _SliverAppBarDelegate( | |
const 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", | |
), | |
], | |
), | |
), | |
), | |
]; | |
}, | |
body: const Center( | |
child: Text("Sample text"), | |
), | |
), | |
), | |
); | |
} | |
} | |
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 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