flutter - 在 TabBarView 中滚动而不影响其他选项卡
问题描述
我试图在一个标签中滚动而不影响其他标签的滚动位置。但我不知道如何实现它。同样在这段代码中,SliverAppBar
当您点击应用栏时,我添加了一个向上滚动。
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with TickerProviderStateMixin {
TabController _tabController;
ScrollController _scrollViewController;
double scrollPosition;
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: 2, initialIndex: 0);
_scrollViewController = ScrollController();
}
@override
void dispose() {
_tabController.dispose();
_scrollViewController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: DefaultTabController(
length: 2,
initialIndex: 0,
child: Scaffold(
body: NestedScrollView(
controller: _scrollViewController,
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
title: InkWell(
onTap: () {
_scrollViewController.animateTo(
0.0,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
},
child: const Text(kCandice, style: kLogoText)),
centerTitle: true,
elevation: 0,
backgroundColor: Colors.white,
pinned: true,
floating: true,
snap: true,
forceElevated: innerBoxIsScrolled,
bottom: TabBar(
tabs: <Tab>[
Tab(
text:
AppLocalizations.of(context).translate('following'),
),
Tab(
text:
AppLocalizations.of(context).translate('trending'),
)
],
unselectedLabelColor: Colors.black54,
labelColor: kPink,
unselectedLabelStyle: kMediumBoldText,
labelStyle: kMediumBoldText,
indicatorSize: TabBarIndicatorSize.tab,
indicatorColor: Colors.transparent,
controller: _tabController,
),
),
];
},
body: TabBarView(
children: [
PostSection(),
PostSection(),
],
),
),
),
),
);
}
}
解决方案
如果这是正确的方法,我不知道,但一种选择是:
@override
void initState() {
_scrollViewController = ScrollController();
_tabController = TabController(vsync: this, length: 2, initialIndex: 0)
..addListener(() => _scrollViewController.animateTo(
0.0,
curve: Curves.easeIn,
duration: const Duration(milliseconds: 300),
));
super.initState();
}
推荐阅读
- shell - 通过 shell 或 linux 命令增加文件名编号
- tomcat - k8s pod启动时启动tomcat后运行脚本
- java - 无法更改 Webview 语言
- mongodb - mongo获取月份和年份范围内的数据
- python - Django Rest - 序列化器:必须是创建时的实例
- mysql - 每月统计客户无法起诉 Laravel 查询生成器
- windows - 在 Windows 10 中以高对比度的暗模式使用 Visual Studio 2019
- rancher - Rancher CLI 登录错误 - 状态 [401 未授权]。正文:[message=clusterID 不匹配]
- c# - 如何从 Windows 窗体应用程序写入文本文件
- r - Mplus vs lavaan 报告的自由参数数量