Flutter ScrollController在NestedScrollView中的位置 [英] Flutter ScrollController position in NestedScrollView

查看:199
本文介绍了Flutter ScrollController在NestedScrollView中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a NestedScrollView that houses a SliverAppBar with a TabBarView and the tabs consist of an infinite loading list each. Right now I only have one ScrollController attached to NestedScrollView, and list widgets read the scroll position of this controller.

无限加载逻辑使用 controller.position.extentAfter 决定何时从API提取数据.但是有多个标签,我得到了错误

The infinite loading logic uses controller.position.extentAfter to decide when to fetch data from the API. But with multiple tabs, I get the error

ScrollController附加到多个滚动视图.

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

I tried reading about controller.positions but couldn't make sense of the 2 line documentation that's available. My question is, is it possible to access scroll positions per page in a TabBarView or should I just use separate ScrollController for each of those and forget about the correct scrolling of slivers?

推荐答案

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

You can't use the same controller for different scroll views. This is the reason you are getting this error "ScrollController attached to multiple scroll views." However, you can listen to the PrimaryScrollController and update your infinite list to fetch more data from the API. If you look at the code of NestedScrollView you would find the PrimaryScrollController there.

    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].

这篇关于Flutter ScrollController在NestedScrollView中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆