简体   繁体   English

ScrollController 附加到多个滚动视图

[英]ScrollController attached to multiple scroll views

I have a stateful widget with children as such我有一个带孩子的有状态小部件

final _scrollController = TrackingScrollController();

PageView(
  controller: _pageController,
  children: <Widget>[
    _ListView(controller: _scrollController),
    _ListView(controller: _scrollController),
    _ListView(controller: _scrollController),
  ],
)

This seems to match the pattern shown here https://docs.flutter.io/flutter/widgets/TrackingScrollController-class.html这似乎与此处显示的模式相匹配https://docs.flutter.io/flutter/widgets/TrackingScrollController-class.html

However, when I scroll one list, the others are not synced, and I get this error for every frame但是,当我滚动一个列表时,其他列表未同步,并且每一帧都会出现此错误

flutter: Another exception was thrown: ScrollController attached to multiple scroll views.

Any ideas as to what I'm doing wrong?关于我做错了什么的任何想法? Do I have unreasonable expectations of TrackingScrollController?我对 TrackingScrollController 有不合理的期望吗?

Please see Ashton Thomas' explanation for why TrackingScrollController doesn't work for this use case.请参阅 Ashton Thomas 的解释,了解为什么TrackingScrollController不适用于此用例。 To achieve the behavior you described, you can use a new package released by Google: linked_scroll_controller .要实现您所描述的行为,您可以使用 Google 发布的新包: linked_scroll_controller

First, add this package to your pubspec.yaml :首先,将此包添加到您的pubspec.yaml

dependencies:
  linked_scroll_controller: ^0.1.2
  // ...

And then integrate it into your code like this:然后像这样将其集成到您的代码中:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final _pageController = PageController();

  // This group keeps track of the synchronized scroll offset.
  final _scrollControllerGroup = LinkedScrollControllerGroup();
  ScrollController _scrollController1;
  ScrollController _scrollController2;
  ScrollController _scrollController3;

  @override
  void initState() {
    super.initState();

    // Create separate ScrollControllers as you need them:
    _scrollController1 = _scrollControllerGroup.addAndGet();
    _scrollController2 = _scrollControllerGroup.addAndGet();
    _scrollController3 = _scrollControllerGroup.addAndGet();
  }

  @override
  void dispose() {
    // Don't forget to dispose all of your controllers!
    _pageController.dispose();
    _scrollController1.dispose();
    _scrollController2.dispose();
    _scrollController3.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PageView(
      controller: _pageController,
      children: <Widget>[
        // Use controllers only once:
        ListView(controller: _scrollController1),
        ListView(controller: _scrollController2),
        ListView(controller: _scrollController3),
      ],
    );
  }
}

Are you getting the scroll position of the _trackingScrollController ?得到_trackingScrollController滚动位置了吗? If so, you will get the error.如果是这样,您将收到错误消息。

Below is the implementation of get postion inside ScrollController which is base for TrackingScrollController .下面是ScrollControllerget postion的实现,它是TrackingScrollController基础。

ScrollPosition get position {
  assert(_positions.isNotEmpty, 'ScrollController not attached to any scroll views.');
  assert(_positions.length == 1, 'ScrollController attached to multiple scroll views.');
  return _positions.single;
 }

The TrackingScrollController is only responsible for producing the initialScrollOffset which is, as you would expect, only the initial offset when being built . TrackingScrollController只负责产生initialScrollOffset ,正如你所期望的,它只是在构建时的初始偏移量

TrackingScrollController just creates a map of ScrollPositions for each ListView and listens to each of these for changes storign this as the new initial scroll position to be used when the next ListView is rendered. TrackingScrollController 只是为每个 ListView 创建一个 ScrollPositions 的映射,并监听每个 ListView 的变化,并将其作为渲染下一个 ListView 时使用的新初始滚动位置。

What is your expected behavior versus what you are seeing?您的预期行为与您所看到的相比是什么? Please provide more information as to your goal and implementation if you can.如果可以,请提供有关您的目标和实施的更多信息。

As for the exception:至于例外:

flutter: Another exception was thrown: ScrollController attached to multiple scroll views.颤振:引发了另一个异常:ScrollController 附加到多个滚动视图。

This is thrown when you use a get the controller.positon while you have multiple positions attached ie you can only use this get when you only ever attach a single positon.当您附加多个位置时使用get controller.positon会抛出此问题,即您只能在仅附加一个位置时使用此 get。

You may want to leverage the get for controller.positions (note the plural, this is an iterable)您可能想利用get获取controller.positions (注意复数,这是一个可迭代的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM