【发布时间】:2021-09-12 16:12:57
【问题描述】:
如何在垂直滚动上切换选项卡,如上所示。最好的方法是什么?单击某个选项卡后,控制权应转移到选定的选项卡内容。我试图在谷歌上找到解决方案,但徒劳无功。
目前我的代码看起来像这样,但我知道不是这样。
class SingleRestView extends StatefulWidget {
Restaurant singleRestaurant;
SingleRestView({@required this.singleRestaurant});
@override
_SingleRestViewState createState() => _SingleRestViewState();
}
class _SingleRestViewState extends State<SingleRestView>
with SingleTickerProviderStateMixin {
var _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
@override
Widget build(BuildContext context) {
var tabBar = TabBar(
controller: _tabController,
//This property will allow your tabs to be horizontally scrollable
isScrollable: true,
indicatorColor: Colors.black,
labelColor: Colors.black,
tabs: [
Text("Tab 1"),
Text("Tab 2"),
Text("Tab 2"),
],
);
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, isScrolled) => [
SliverAppBar(
expandedHeight: 300,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('Title'),
//Without this padding the title appears behind the tabs.
//This is the whole reason why the tabBar is in a local variable, to
//be able to use its height here.
titlePadding: EdgeInsetsDirectional.only(
start: 72, bottom: tabBar.preferredSize.height + 16),
// background:
),
// I did this this way to have a white bottom bar like in your photo,
// however, you could just pass the tabBar. The background image would
//be behind it.
bottom: PreferredSize(
child: Container(
//This will keep your tabs centered if you have not enough to fill
//the display's width
alignment: Alignment.center,
width: double.infinity,
color: Colors.white,
child: tabBar,
),
preferredSize: Size(double.infinity, tabBar.preferredSize.height),
),
),
],
body: TabBarView(
controller: _tabController,
children: <Widget>[
Container(height: 50,width: 100,color: Colors.red,),
Container(height: 50,width: 100,color: Colors.blue,),
Container(height: 50,width: 100,color: Colors.green,),
],
),
),
);
}
}
【问题讨论】:
-
嗨 Faizan,我列出了一些可能的选项,还附上了一些进一步的参考资料供阅读,我建议你一定要检查一下。谢谢你的问题:)
标签: flutter dart scroll scrollview