如何在颤动列表视图中通过索引编号滚动到索引?

如何在颤动列表视图中通过索引编号滚动到索引?,第1张

如何在颤动列表视图中通过索引编号滚动到索引?

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

animateTo()
或的元素偏移量
jumpTo()
,或者可以从其他帖子中搜索建议的解决方案/插件,例如:

  • 颤振ListView滚动到索引不可用
  • Flutter:滚动到ListView中的小部件

自2017年以来, 在flutter / issues /
12319上
讨论了一般的scrollToIndex问题,但目前尚无计划)


但是还有另一种不支持scrollToIndex的ListView:

  • ScrollablePositionedList
    • scrollable_positioned_list
    • 依赖:flutter_widgets

您可以像设置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核心团队开发的。)



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5020643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存