flutter - Flutter - 如何同步两个或多个 PageController
问题描述
在Flutter
我不能PageController
对许多人说同样的话PageView
。所以需要使用两个或多个PageController
s。
我需要同步我ViewPage
的 s 以便当我相互滑动时它也会滑动
如何同步两个或更多PageController
或PageView
?
我想要的是每个人都PageView
可以控制其他人PageView
,让他们无论在哪张幻灯片上都同步。
因此,如果 a 有A、B和C PageView
并且我滑动A 然后B和C也滑动......如果我滑动B然后A和C滑动......等等。
解决方案
可以参考https://github.com/braulio94/menu_flutter
诀窍是使用 NotificationListener 监听 ScrollUpdateNotification
并比较两个页面
_backgroundPageController.page != _pageController.page
第 183 行的代码片段
new NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification.depth == 0 &&
notification is ScrollUpdateNotification) {
selectedIndex.value = _pageController.page;
if (_backgroundPageController.page != _pageController.page) {
_backgroundPageController.position
// ignore: deprecated_member_use
.jumpToWithoutSettling(_pageController.position.pixels /
_kViewportFraction);
}
setState(() {});
}
return false;
}
完整代码
https://github.com/braulio94/menu_flutter/blob/master/lib/screens/pager.dart
推荐阅读
- python - 为什么我得到 IndexError: list index out of range
- facebook-graph-api - FacebookAds\Exception\Exception,代码:35:LibreSSL SSL_connect:SSL_ERROR_SYSCALL 连接到 graph.facebook.com:443
- view - 使用 DocumentPicker SwiftUI 更改状态
- vim - vim 标签:如何获取
与 tselect 一起行动 - google-cloud-platform - 从 Blob 对象调用 copyTo 方法时,GCP SDK 是否下载文件?
- r - 在创建函数时需要帮助
- tensorflow - 导入 tflearn 时没有名为“tensorflow.contrib”的模块
- c++ - 如何在不使用“goto”和使用“do while”循环的情况下进入程序的初始阶段?
- excel-formula - 用公式中的 NOT 函数替换 AND
- r - 计算字符串中以特定顺序出现的特定单词