【发布时间】:2021-01-26 11:42:30
【问题描述】:
我在没有TabBarView 的情况下实现了TabBar。我使用单个ListView 作为body,因为选择选项卡后的布局对于所有选项卡都是相同的。
我想要实现的是,在列表视图中向左/向右滑动时更改选项卡。我该怎么做?
标签栏
TabBar(
indicatorWeight: 3,
indicatorSize: TabBarIndicatorSize.label,
onTap: (index) {
categoryId = newsProvider.categories[index].id;
page = 1;
fetchPosts(newsProvider);
},
isScrollable: true,
tabs: [
for (Category category in newsProvider.categories)
Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: Text(
category.name,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
),
],
),
正文
ListView.builder(
padding: EdgeInsets.only(bottom: 60),
physics: BouncingScrollPhysics(),
controller: _scrollController,
itemCount: newsProvider.posts.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
HabaruDetails(newsProvider.posts[index]),
));
},
child: Container(
height: 200,
margin: EdgeInsets.only(left: 10, right: 10, top: 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(7),
child: Stack(
children: [
Positioned.fill(
child: Hero(
tag: newsProvider.posts[index].id,
child: FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
fit: BoxFit.cover,
image: newsProvider
.posts[index].betterFeaturedImage.mediumLarge),
),
),
Container(
height: 200,
decoration: BoxDecoration(
color: Colors.white,
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
Colors.black.withOpacity(0.0),
Colors.black.withOpacity(0.95),
],
stops: [
0.0,
1.0
])),
),
Positioned.fill(
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 30),
child: Text(
newsProvider.posts[index].title.rendered,
textAlign: TextAlign.center,
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
height: 1.7,
),
),
),
),
),
],
),
),
),
);
},
)
【问题讨论】:
-
你的意思是水平滑动还是垂直滑动?
-
我要水平滑动
-
我的意思是你只有1个垂直列表但你想水平交换它,所以预期的行为是通过滑动来改变标签栏索引和列表内容或者只是改变标签索引,对不起对于长时间的提问,但如果我更了解您,我可能会有所帮助。
-
你做对了。我想通过向左或向右滑动列表来更改选项卡索引。当标签索引更改时,我将调用 API 来获取新数据并加载到列表中。我不希望每个选项卡都有单独的列表视图。
-
我不知道我是否应该为此写一个完整的答案,但解决方案很简单,您可以使用 GestureDetector 扭曲列表并检测滑动或使用 swipedetector 包链接:pub.dev/packages/swipedetector ,基本上它会为你提供 onSwipeLeft 和 onSwipeRight 函数,所以只需一些逻辑来传递标签索引就会得到你想要的。如果您需要进一步解释,我可以为您编写代码。