颤振ListView滚动到索引不可用 [英] flutter ListView scroll to index not available
问题描述
我需要什么:
我想按某个索引滚动列表,我该怎么做。
I want to scroll a list by some index, how can i do that.
我所知道的:
scrollToIndex
应该从n个索引开始,但是我们如何滚动到任何索引?
scrollToIndex
should start from n index, but how can we scroll to any index?
推荐答案
不幸的是,ListView没有内置的方法来scrollToIndex()函数。您必须开发自己的方法来测量 animateTo()
或 jumpTo()
元素的偏移量,或者您可以搜索这些建议的解决方案/插件或其他帖子,例如 Flutter:滚动到ListView中的小部件
Unfortunately, ListView has no built-in approach to a scrollToIndex() function. You’ll have to develop your own way to measure to that element’s offset for animateTo()
or jumpTo()
, or you can search through these suggested solutions/plugins or from other posts like Flutter: Scrolling to a widget in ListView
(有关一般scrollToIndex问题,请参见 flutter / issues / 12319 自2017年以来,但仍没有当前计划)
(the general scrollToIndex issue is discussed at flutter/issues/12319 since 2017, but still with no current plans)
,但是有另一种ListView确实支持scrollToIndex(如Slashbin所述 ):
But there is a different kind of ListView that does support scrollToIndex (as mentioned by Slashbin):
- ScrollablePositionedList
- ScrollablePositionedList
- dependency: scrollable_positioned_list
您可以像设置ListView一样来设置它,并且工作原理相同,只是现在您可以访问 ItemScrollController 可以做到:
You set it up exactly like ListView and works the same, except you now have access to a ItemScrollController that does:
-
jumpTo({index ,alignment})
-
scrollTo({index,alignment,duration,curve})
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核心团队开发的。)
这篇关于颤振ListView滚动到索引不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- ScrollablePositionedList