NestedScrollView中的Flutter ScrollController位置

时间:2018-12-16 08:52:09

标签: flutter

我有一个NestedScrollView,其中包含一个SliverAppBar和一个TabBarView,每个选项卡均包含一个无限的加载列表。现在,我只在ScrollController上附加了一个NestedScrollView,并且列表小部件读取了此控制器的滚动位置。

无限加载逻辑使用controller.position.extentAfter来决定何时从API提取数据。但是在多个标签中,出现了错误

ScrollController附加到多个滚动视图。

我尝试阅读有关controller.positions的内容,但无法理解现有的两行文档。我的问题是,是否可以在TabBarView中访问每页的滚动位置,还是我应该仅对每个页面使用单独的ScrollController并忘记正确地滚动条子?

1 个答案:

答案 0 :(得分:3)

您不能将同一控制器用于不同的滚动视图。这就是您收到此错误“ ScrollController附加到多个滚动视图。”的原因。 但是,您可以侦听PrimaryScrollController并更新您的无限列表以从API中获取更多数据。如果您查看NestedScrollView的代码,则会在其中找到PrimaryScrollController。

    NestedScrollView(
      body: Builder(
        builder: (BuildContext context) {
          final innerScrollController = PrimaryScrollController.of(context);

          // Use the innerScrollController to listen to the scrolling.
// This would be your controller for list. You can listen to this controller to know whether the list has reached maxScrollExtent and fetch data from API.

        }
      ),
    );

供参考:

/// The [body] is built in a context that provides a [PrimaryScrollController]
  /// that interacts with the [NestedScrollView]'s scroll controller. Any
  /// [ListView] or other [Scrollable]-based widget inside the [body] that is
  /// intended to scroll with the [NestedScrollView] should therefore not be
  /// given an explicit [ScrollController], instead allowing it to default to
  /// the [PrimaryScrollController] provided by the [NestedScrollView].