如何删除 SliverAppBar 中的空 space?
How to remove empty space in SliverAppBar?
我在这里使用 NestedScrollView
并尝试将 TabBar
放入 SliverAppBar
的 bottom
参数中,该参数内置于 headerSliverBuilder
函数中. TabBar
工作正常,但看起来 SliverAppBar
已经避免了 title
一些 space,这使得标签栏看起来很奇怪,上面有大填充。知道如何删除这个 space 吗?提前致谢。
GIF
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
return Scaffold(
body: SafeArea(
child: NestedScrollView(
headerSliverBuilder: (context, value){
return[
SliverOverlapAbsorber(
handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
pinned: true,
floating: false,
expandedHeight: 500,
title: Text("Space that I don't want"),
centerTitle: true,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Container(
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage("assets/images/classroom.png")
)
),
child: Center(child: Text("Today's lesson cancelled", style: GoogleFonts.montserrat(textStyle: TextStyle(color: Colors.white, fontSize:20, fontWeight: FontWeight.w500),)))
),
),
bottom: TabBar(
controller: _controller,
labelColor: Colors.black,
indicatorColor: Colors.black,
unselectedLabelColor: Colors.grey,
tabs: _tabs.map((String name) => Tab(text: name)).toList()
)
)
),
];
},
body: TabBarView(
controller: _controller,
children: (_tabs.map((String name){
return SafeArea(
child:Builder(
builder: (BuildContext context){
return CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
SliverOverlapInjector(
handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
),
SliverToBoxAdapter(
child: _buildLeaderBoardTab(screenWidth,screenHeight)
)
],
);
},
)
);
})).toList(),
)
)
)
);
}
您可以使用高度为 0 的 PreferredSize
小部件包裹标签栏:
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
context),
sliver: SliverAppBar(
bottom: PreferredSize(
preferredSize: const Size.fromHeight(0),
child: TabBar(
controller: _controller,
labelColor: Colors.black,
indicatorColor: Colors.black,
unselectedLabelColor: Colors.grey,
tabs: _tabs
.map((String name) => Tab(text: name))
.toList()),
),
pinned: true,
floating: false,
expandedHeight: 500,
backgroundColor: Colors.white,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Container(
child: child: Center(child: Text("Today's lesson cancelled", style: GoogleFonts.montserrat(textStyle: TextStyle(color: Colors.white, fontSize:20, fontWeight: FontWeight.w500),)))
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
fit: BoxFit.cover,
image: DecorationImage(
image: AssetImage("assets/images/classroom.png")
)
),
)),
注意:背景图片我使用了fit: BoxFit.cover
,这就是标签栏没有白色背景的原因。
结果:
我在这里使用 NestedScrollView
并尝试将 TabBar
放入 SliverAppBar
的 bottom
参数中,该参数内置于 headerSliverBuilder
函数中. TabBar
工作正常,但看起来 SliverAppBar
已经避免了 title
一些 space,这使得标签栏看起来很奇怪,上面有大填充。知道如何删除这个 space 吗?提前致谢。
GIF
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
return Scaffold(
body: SafeArea(
child: NestedScrollView(
headerSliverBuilder: (context, value){
return[
SliverOverlapAbsorber(
handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
pinned: true,
floating: false,
expandedHeight: 500,
title: Text("Space that I don't want"),
centerTitle: true,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Container(
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
image: AssetImage("assets/images/classroom.png")
)
),
child: Center(child: Text("Today's lesson cancelled", style: GoogleFonts.montserrat(textStyle: TextStyle(color: Colors.white, fontSize:20, fontWeight: FontWeight.w500),)))
),
),
bottom: TabBar(
controller: _controller,
labelColor: Colors.black,
indicatorColor: Colors.black,
unselectedLabelColor: Colors.grey,
tabs: _tabs.map((String name) => Tab(text: name)).toList()
)
)
),
];
},
body: TabBarView(
controller: _controller,
children: (_tabs.map((String name){
return SafeArea(
child:Builder(
builder: (BuildContext context){
return CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
SliverOverlapInjector(
handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
),
SliverToBoxAdapter(
child: _buildLeaderBoardTab(screenWidth,screenHeight)
)
],
);
},
)
);
})).toList(),
)
)
)
);
}
您可以使用高度为 0 的 PreferredSize
小部件包裹标签栏:
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
context),
sliver: SliverAppBar(
bottom: PreferredSize(
preferredSize: const Size.fromHeight(0),
child: TabBar(
controller: _controller,
labelColor: Colors.black,
indicatorColor: Colors.black,
unselectedLabelColor: Colors.grey,
tabs: _tabs
.map((String name) => Tab(text: name))
.toList()),
),
pinned: true,
floating: false,
expandedHeight: 500,
backgroundColor: Colors.white,
flexibleSpace: FlexibleSpaceBar(
collapseMode: CollapseMode.pin,
background: Container(
child: child: Center(child: Text("Today's lesson cancelled", style: GoogleFonts.montserrat(textStyle: TextStyle(color: Colors.white, fontSize:20, fontWeight: FontWeight.w500),)))
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
fit: BoxFit.cover,
image: DecorationImage(
image: AssetImage("assets/images/classroom.png")
)
),
)),
注意:背景图片我使用了fit: BoxFit.cover
,这就是标签栏没有白色背景的原因。
结果: