我想要的是,当我向上滚动GridView时,轮播会向上滚动,并且选项卡控制器保持在顶部固定位置。所有这些都应在一个滚动中完成。我之前已经使用自定义滚动和切片GridView完成了此操作,但是我不知道如何在自定义scrollView中添加Default Tab控制器并使它固定在顶部。
感谢您的帮助:)
答案 0 :(得分:2)
我尝试了下面的代码,希望它可以解决您的问题。...
您也可以在DartPad NestedScrollView
上使用此代码NestedScrollView(
physics: ClampingScrollPhysics(),
headerSliverBuilder: (context, value) {
return [
SliverToBoxAdapter(
/// _buildCarousel() in your case....
child: Container(
height: 200,
child: Center(
child: Text("Your Carousel will be here"),
)
),
),
SliverToBoxAdapter(
child: TabBar(
labelColor: Colors.blue,
unselectedLabelColor: Colors.black,
controller: tb,
tabs: <Widget>[
Tab(child: Text("tab1"),),
Tab(child: Text("tab2"),)
],
)
),
];
},
body: TabBarView(
controller: tb,
children: <Widget>[
GridView.count(
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 3,
children: List.generate(10,
(index) => Icon(Icons.grid_off)
).toList()
),
GridView.count(
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 3,
children: List.generate(5,
(index) => Icon(Icons.grid_on)
).toList()
),
],
),
)
答案 1 :(得分:2)
有关运行ex的信息,请参见此DartPad。 PinnedAppBar_SliverAppBar_NestedScrollView
NestedScrollView(
controller: ScrollController(),
physics: ClampingScrollPhysics(),
headerSliverBuilder: (context, value) {
return [
SliverAppBar(
pinned: true,
backgroundColor: Colors.white,
flexibleSpace: FlexibleSpaceBar(
background:
/// _buildCarousel() in your case....
Container(
height: 200,
child: Center(
child: Text("Your Carousel will be here"),
)
),
),
expandedHeight: 250.0, /// your Carousel + Tabbar height(50)
floating: true,
bottom: TabBar(
labelColor: Colors.blue,
unselectedLabelColor: Colors.black,
controller: tb,
tabs: <Widget>[
Tab(child: Text("tab1"),),
Tab(child: Text("tab2"),)
],
),
),
];
},
body: TabBarView(
controller: tb,
children: <Widget>[
GridView.count(
crossAxisCount: 3,
children: List.generate(10,
(index) => Icon(Icons.grid_off)
).toList()
),
GridView.count(
crossAxisCount: 3,
children: List.generate(5,
(index) => Icon(Icons.grid_on)
).toList()
),
],
),
)