fluttercandies / flutter_scrollview_observer

A widget for observing data related to the child widgets being displayed in a ScrollView. Maintainer: @LinXunFeng
https://pub.dev/packages/scrollview_observer
MIT License
421 stars 44 forks source link

如何获取使用sticky_header时的索引 #71

Closed yyong37 closed 7 months ago

yyong37 commented 7 months ago

Platforms

dart

Description

example:multi_sliver_demo_page

请问在该示例中,我该如何获取section的索引?

My code

No response

Try do it

No response

LinXunFeng commented 7 months ago

sliver 的下标无法直接拿到,需要你自己记录与维护一个 Map<BuildContext, int>,存储的内容为:{sliverCtx : index},再结合 onObserveViewport 回调可以获取当前的第一个 sliverCtx,通过这种方式可以间接拿到索引。

yyong37 commented 7 months ago

谢谢你的解答。我现在按照你说的在onObserveViewPort中没有任何输出,是我忽略了什么东西吗?

SliverViewObserver(
        controller: sliverObserverController,
        sliverContexts: () => contextList,
// this line
        onObserveViewport: (result) {
          final firstChildCtxInViewport = result.firstChild.sliverContext;
          debugPrint('first child ctx-- $firstChildCtxInViewport');
        },
        child: CustomScrollView(......)
LinXunFeng commented 7 months ago

很有可能是你传入的 sliverContexts 用的不是最外层的 sliver 相关说明:1.3、onObserveViewport 回调

建议你用两个 SliverViewObserver,一个负责观察 item,另一个负责观察 sliver,这样也能区分开 sliverContexts