如何使用PrimaryScrollController从NestedScrollView内部的CustomScrollView获取偏移量

Zap*_*Zap 7 flutter flutter-layout

我有NestedScrollView一个SliverAppBar。的身体NestedScrollView是有一个小部件CustomScrollViewSliverList

我想从ScrollController给予的偏移量,NestedScrollView但是它只给我偏移量,直到看不见为止SliverAppBar,但是此后,SliverList继续滚动但ScrollController没有给出偏移量。

NestedScrollView(
  headerSliverBuilder: (_, __) => SliverAppBar(..),
  body: RefreshIndicator(
    child: CustomScrollView(..),
  ),
)
Run Code Online (Sandbox Code Playgroud)

CustomScrollView会自动使用PrimaryScrollController提供NestedScrollView
当侦听器附加到该滚动视图时,当SliverAppBar幻灯片滑入和滑出视图时,将调用该侦听器,直到position.extentAfter等于0.0,然后再也不会更新,无论您向下滚动多远,因为extentAfter始终保持在0.0

如何获取CustomScrollView内部的滚动位置NestedScrollView


实作

之所以重要,PrimaryScrollController是因为ScrollController应当具有的功能PrimaryScrollController。无论如何,为了SliverAppBar使它起作用(即,它与内容一起滚动出视图),该NestedScrollView controller参数需要以某种方式与CustomScrollView controller(或primary)参数匹配。

这是一个演示

任何允许内容滚动的实现都可以PrimaryScrollController.of(context)
请注意,NestedScrollView必不可少,以便RefreshIndicator出现在下方SliverAppBar

WiR*_*ght 4

也许我可以帮助你!

Widget build(BuildContext context) {
  return Scaffold(
    body: NestendScrollView(
      headerSliverBuilder: (BuildContext context, bool value) {
        return <Widget>[
           SliverAppBar(),
        ];
      },
        body: SafeArea(
          child: Builder(
            builder: (context) {
              final _scr = PrimaryScrollController.of(context);
              _scr.addListener(() {
                if (_scr.position.pixels == _scr.position.maxScrollExtent) {
                  print('At DOWNW!!!');
                }
              });

              return CustomScrollView(
                controller: _scr,
                slivers: <Widget>[
                  SliverOverlapAbsorber(
                    handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                      context,
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildListDelegate(
                      List.generate(100, (int index) {
                        return Text('ITEM -> $index');
                      }),
                    ),
                  )
                ],
              );
            },
          ),
        ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)