如何在ListView.builder中保持当前滚动位置,而不在添加新项目时自动滚动。

huangapple go评论79阅读模式
英文:

How to keep at the current Scroll position in ListView.builder without any auto scroll when new items added

问题

使用情况:我正在制作聊天页面的分页,将"reverse"设置为false,并且当用户向上滚动以加载更多项目时,滚动会自动跳转到0偏移量,如何防止这种情况发生?我希望无论添加了什么项目,都能保持在当前位置。

我对这个问题进行了很多研究,找到的解决方案场景如下:

  • 将"reverse"设置为true:但这不适用于我的情况,因为我还有双向分页

  • 计算添加项目前后的高度差:但我发现这太复杂了,而且不总是准确的(如果有准确的计算方法,请分享)

有没有解决这个非常恼人行为的解决方案?

英文:

the use case : i am making pagination in chat page, the reverse set to false , and when user scroll up to load more items the scroll auto jump to 0 offset , How to prevent it ? i want like whatever items added so want to stay at the same current position .

I researched a lot about this problem and the solution scenarios I found like following

  • reverse set to true : but this not suite my case because i also have two side pagination too

  • Calculate the height difference between before and after items added : but i found this so
    complicated And not always accurate (Any accurate calculation for this thing, please share)

any solution guys with this very annoying behavior?

答案1

得分: 1

如果您正在使用ListView.builder小部件中的key属性,请尝试跳过该属性。我遇到了同样的问题,并使用这种方式解决了它。这样,在添加新项目时,它会保持其位置。

英文:

If you are using the key attribute inside your ListView.builder widget. Try skipping that attribute. I was having the same problem and fixed it using this. In this way, it keeps its position when new items are added.

huangapple
  • 本文由 发表于 2023年6月12日 03:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定