首页 > 解决方案 > Flutter - 如何同步两个或多个 PageController

问题描述

Flutter我不能PageController对许多人说同样的话PageView。所以需要使用两个或多个PageControllers。

我需要同步我ViewPage的 s 以便当我相互滑动时它也会滑动

如何同步两个或更多PageControllerPageView

我想要的是每个人都PageView可以控制其他人PageView,让他们无论在哪张幻灯片上都同步。

因此,如果 a 有ABC PageView并且我滑动A 然后BC也滑动......如果我滑动B然后AC滑动......等等。

标签: flutter

解决方案


可以参考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

在此处输入图像描述


推荐阅读