Listview 如何在颤振中同时使用SliverPersistentHeader和TabBarView?
我有一个带有Listview 如何在颤振中同时使用SliverPersistentHeader和TabBarView?,listview,flutter,tabbar,flutter-sliver,Listview,Flutter,Tabbar,Flutter Sliver,我有一个带有SliverPersistentHeader和TabBarView的主页,如下代码: @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Theme.of(context).backgroundColor, body: DefaultTabController( length: 3, child: Nest
SliverPersistentHeader
和TabBarView
的主页,如下代码:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: DefaultTabController(
length: 3,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 250.0,
floating: false,
pinned: true,
),
SliverPersistentHeader(
delegate: _SliverAppBarDelegate(
TabBar(
labelStyle: TextStyle(
fontFamily: 'Raleway',
fontSize: 17,
fontWeight: FontWeight.w400,
color: Theme.of(context).backgroundColor),
indicatorColor: Theme.of(context).hintColor,
labelColor: Theme.of(context).buttonColor,
unselectedLabelColor: Theme.of(context).dividerColor,
tabs: [
Tab(text: "Menu"),
Tab(text: "About"),
Tab(text: "Contact"),
],
),
),
pinned: true,
),
];
},
body: TabBarView(
children: <Widget>[MenuTab(), AboutTab(), ContactTab()],
),
),
),
);
}
}
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;
}
}
class _AboutTabState extends State<AboutTab> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: ListView(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: _list.length,
itemBuilder: (BuildContext context, int index) {
final _aboutList = _list[index];
return ExpansionTile(
title: ListTile(
title: Padding(
padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
child: Text(_aboutList.aboutTitle,
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).buttonColor)),
),
),
children: <Widget>[
ListTile(
title: Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 0, 0),
child: Text(_aboutList.content,
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 16,
fontWeight: FontWeight.w400,
color: Theme.of(context).toggleableActiveColor)),
),
)
],
);
},
),
],
));
@覆盖
小部件构建(构建上下文){
返回脚手架(
背景色:主题。背景色,
正文:DefaultTabController(
长度:3,
子:嵌套滚动视图(
headerSliverBuilder:(BuildContext上下文,boolInnerBoxIsCrolled){
返回[
滑杆(
扩展高度:250.0,
浮动:假,
对,,
),
滑冰机(
代表:_SliverAppBarDelegate(
塔巴(
标签样式:文本样式(
fontFamily:“Raleway”,
尺寸:17,
fontWeight:fontWeight.w400,
颜色:主题(上下文)。背景色),
指示色:主题(上下文).hintColor,
标签颜色:主题。背景。按钮颜色,
未选择的标签颜色:主题.of(上下文).dividerColor,
选项卡:[
选项卡(文本:“菜单”),
选项卡(文本:“关于”),
选项卡(文本:“联系人”),
],
),
),
对,,
),
];
},
正文:选项卡视图(
子项:[MenuTab()、AboutTab()、ContactTab()],
),
),
),
);
}
}
类_SliverAppBarDelegate扩展SliverPersistentHeaderDelegate{
_SliverAppBarDelegate(此选项卡);
最终TabBar _TabBar;
@凌驾
double-get-minExtent=>\u tabBar.preferredSize.height;
@凌驾
double get maxExtent=>\u tabBar.preferredSize.height;
@凌驾
小部件构建(
BuildContext上下文、双收缩偏移、布尔重叠内容){
退回新货柜(
孩子:_tabBar,
);
}
@凌驾
布尔应该重建(\u SliverAppBarDelegate oldDelegate){
返回false;
}
}
之后,我为每个选项卡创建了单个类,例如下面的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: DefaultTabController(
length: 3,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 250.0,
floating: false,
pinned: true,
),
SliverPersistentHeader(
delegate: _SliverAppBarDelegate(
TabBar(
labelStyle: TextStyle(
fontFamily: 'Raleway',
fontSize: 17,
fontWeight: FontWeight.w400,
color: Theme.of(context).backgroundColor),
indicatorColor: Theme.of(context).hintColor,
labelColor: Theme.of(context).buttonColor,
unselectedLabelColor: Theme.of(context).dividerColor,
tabs: [
Tab(text: "Menu"),
Tab(text: "About"),
Tab(text: "Contact"),
],
),
),
pinned: true,
),
];
},
body: TabBarView(
children: <Widget>[MenuTab(), AboutTab(), ContactTab()],
),
),
),
);
}
}
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;
}
}
class _AboutTabState extends State<AboutTab> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: ListView(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: _list.length,
itemBuilder: (BuildContext context, int index) {
final _aboutList = _list[index];
return ExpansionTile(
title: ListTile(
title: Padding(
padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
child: Text(_aboutList.aboutTitle,
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 16,
fontWeight: FontWeight.w500,
color: Theme.of(context).buttonColor)),
),
),
children: <Widget>[
ListTile(
title: Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 0, 0),
child: Text(_aboutList.content,
style: TextStyle(
fontFamily: 'Raleway',
fontSize: 16,
fontWeight: FontWeight.w400,
color: Theme.of(context).toggleableActiveColor)),
),
)
],
);
},
),
],
));
class\u关于bstate扩展状态
函数_SliverAppBarDelegate()返回一个容器,您可以像这样在其中添加颜色:
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(
color: Colors.white, // ADD THE COLOR YOU WANT AS BACKGROUND.
child: _tabBar,
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return false;
}
}