如何在Flutter中将TabBar添加到SliverAppBar?到目前为止,当我向SliverAppBar添加bottom
时,title
会固定到这些标签,而我希望它位于这些标签之上。
有什么想法吗?
我的代码:
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
TabController controller;
@override
void initState() {
super.initState();
controller = new TabController(length: 3, vsync: this);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new CustomScrollView(
slivers: <Widget>[
new SliverAppBar(
pinned: true,
flexibleSpace: new FlexibleSpaceBar(
title: new Text("Some title"),
),
expandedHeight: 160.0,
bottom: new TabBar(tabs: [
new Tab(text: 'Tab 1'),
new Tab(text: 'Tab 2'),
new Tab(text: 'Tab 3'),
],
controller: controller,
),
),
new SliverList(
delegate: new SliverChildBuilderDelegate(
(context, idx) {
return new ListTile(
title: new Text("$idx"),
);
},
childCount: 20,
),
),
],
),
);
}
}
答案 0 :(得分:7)
几周前我遇到了同样的问题,可以使用SliverAppBar
,FlexibleSpaceBar
和SliverPersistentHeader
来解决。
检查我的代码
@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: 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(
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: Center(
child: Text("Sample text"),
),
),
),
);
}
这是SliverPersistentHeader的实现
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;
}
}
如果您要检查,我写了一篇关于它的文章:
https://medium.com/@diegoveloper/flutter-collapsing-toolbar-sliver-app-bar-14b858e87abe
答案 1 :(得分:1)
FlexibleSpaceBar堆叠在工具栏和标签栏后面。 https://docs.flutter.io/flutter/material/SliverAppBar/flexibleSpace.html
您应该将标题放在SilverAppBar中:标题不在flexibleSpace中。
在您的代码中替换
flexibleSpace: new FlexibleSpaceBar(
title: new Text("Some title"),
),
与
title: new Text("Some title"),
答案 2 :(得分:1)
您可以使用titlePadding进行填充:
SliverAppBar(
pinned: true,
expandedHeight: 250,
primary: true,
forceElevated: true,
flexibleSpace: FlexibleSpaceBar(
titlePadding: EdgeInsets.only(bottom: 62),
title: Text(data.name),
centerTitle: true,
background: data.logo != null
? Padding(
padding: const EdgeInsets.only(bottom: 46),
child: Container(
color: Colors.black,
),
)
: null,
),
bottom: TabBar(
tabs: [
Tab(text: "Tab1"),
Tab(text: "Tab2"),
Tab(text: "Tab3"),
Tab(text: "Tab4"),
],
),
),
答案 3 :(得分:0)
class Menu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: SafeArea(
child: Scaffold(
body: Consumer<MenuBlock>(
builder: (context, appController, child) {
return CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: true,
pinned: false,
snap: true,
forceElevated: true,
elevation: 1,
expandedHeight: 50.0,
title: Text('TITLE'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.add_circle),
tooltip: 'Add new entry',
onPressed: () {/* ... */},
),
],
bottom: TabBar(
tabs: [
Tab(text: "TAB1"),
Tab(text: "TAB2"),
],
),
),
SliverList(
delegate: SliverChildListDelegate(
<Widget>[
Container(
// or SizedBox, etc.
height: 4200,
child: TabBarView(
children: [
),
),
],
),
),
],
);
}),
),
),
);
}
}