Flutter ScrollController 在 NestedScrollView 中的位置

ish*_*aan 2 flutter

我有一个NestedScrollView容纳有SliverAppBarTabBarView和标签由每一个无限加载列表。现在我只有一个ScrollController附加到NestedScrollView,并且列表小部件读取此控制器的滚动位置。

无限加载逻辑用于controller.position.extentAfter决定何时从 API 获取数据。但是使用多个选项卡,我收到错误

ScrollController 附加到多个滚动视图。

我尝试阅读,controller.positions但无法理解可用的 2 行文档。我的问题是,是否可以在 a 中访问每页的滚动位置,TabBarView还是应该ScrollController为每个页面单独使用而忘记正确滚动条子?

Deb*_*kar 5

您不能为不同的滚动视图使用相同的控制器。这就是您收到此错误“ 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.

        }
      ),
    );
Run Code Online (Sandbox Code Playgroud)

以供参考 :

/// 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].
Run Code Online (Sandbox Code Playgroud)