小编典典

颤振ListView滚动到索引不可用

flutter

我需要什么:

我想按某个索引滚动列表,我该怎么做。

我知道的:

scrollToIndex 应该从n索引开始,但是如何滚动到任何索引?


阅读 592

收藏
2020-08-13

共1个答案

小编典典

不幸的是,ListView没有对scrollToIndex()函数的内置方法。您必须开发自己的方法来测量animateTo()或的元素偏移量jumpTo()


但是还有另一种ListView确实支持scrollToIndex( 如Slashbin所述 ):

您可以像设置ListView一样进行设置,并且工作原理相同,除了现在可以访问执行以下操作的ItemScrollController之外

  • jumpTo({index, alignment})
  • scrollTo({index, alignment, duration, curve})

简化示例:

ItemScrollController _scrollController = ItemScrollController();

ScrollablePositionedList.builder(
  itemScrollController: _scrollController,
  itemCount: _myList.length,
  itemBuilder: (context, index) {
    return _myList[index];
  },
)

_scrollController.scrollTo(index: 150, duration: Duration(seconds: 1));

(请注意,该库是由Google开发的,而不是由Flutter核心团队开发的。)

2020-08-13