在Flutter中使用ListView或CustomScrollView在顶部添加新元素时如何保持滚动位置 [英] How to keep the scrolling position when adding a new elements in the top using ListView or CustomScrollView in Flutter

查看:1516
本文介绍了在Flutter中使用ListView或CustomScrollView在顶部添加新元素时如何保持滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将新元素添加到ListVeiw或CustomScrollView的顶部,并保持滚动位置与Flutter一样

Is it possible to add new elements into the top of the ListVeiw or CustomScrollView and keep the scroll position as it's with Flutter

我尝试了以下

extentAfter = _scrollController.position.extentAfter;
_scrollController.jumpTo(_scrollController.position.maxScrollExtent - extentAfter);

,但这与SliverAppBar不能很好地配合使用-浮动且实际上不是最佳解决方案。

but this won't work well with SliverAppBar - floating and its actually not optimal solution.

推荐答案

实际上,最干净的方法是仅在用户滚动回到ListView顶部时才添加这些项目。当用户离列表顶部(在您的情况下)足够远时,可以将它们缓存在内存中。

Actually the cleanest way would be to add those items only when user scrolls back to the top of the ListView. You can cache them in memory for the moment user is far enough from the top (in your case) of the list.

这篇关于在Flutter中使用ListView或CustomScrollView在顶部添加新元素时如何保持滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆