gpt4 book ai didi

Flutter - 如何同步两个或多个 PageController

转载 作者:行者123 更新时间:2023-12-05 05:09:23 26 4
gpt4 key购买 nike

Flutter 中,我无法将相同的 PageController 分配给多个 PageView。所以需要使用两个或多个 PageController

我需要同步我的 ViewPage,这样当我互相滑动时它也会滑动

如何同步两个或多个 PageControllerPageView

我想要的是每一个 PageView 都可以控制其他 PageView 无论哪张幻灯片都同步。

所以如果 a 有 ABC PageView 并且我滑动 A 然后 BC 也滑动...如果我滑动 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

enter image description here

关于Flutter - 如何同步两个或多个 PageController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com