颤动 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
您的设置与 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})
简化示例:
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屋!