如何在向前插入项目时保持 Flutter PageView 的状态?
我从后端获取一些视频数据并使用 PageView 来显示它们。视频数据是从后端分页获取的,在某些情况下,我们需要向前翻页。所以我向前插入视频数据并调用setState()来重建PageView。这种情况下如果想保持当前视频页面的状态该怎么办?
我尝试使用 Key()
和 AutomaticKeepAliveClientMixin
来解决它以保持当前页面的状态,但它不起作用。我想可能是因为当前页面的索引发生了变化,所以必须重新构建。还有其他方法可以满足要求吗?谢谢!!
I get some video data from backend and use PageView to show them. The video data is obtained by paging from backend.In some situations, we need to turn the page forward. So I insert video data forward and call setState() to rebuild PageView. What should I do if I want to keep the state of current video page in this situation?
I try to resolve it using Key()
and AutomaticKeepAliveClientMixin
to keep state of current page, but it did not work. I think it may be because the index change of the current page, so it must be rebuilt. Is there any other way to meet the requirement? Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pageIndex
,并在 initState 方法中尝试调用另一个方法,例如_loadData()
来获取数据。pageController
更改时才更新pageIndex
的状态。pageIndex
变量提供 PageView 小部件的initialIndex
。pageIndex
的状态不会改变。pageIndex
in a variable and in the initState method try calling another method like_loadData()
which get's the data.pageIndex
only when thepageController
is changed.initialIndex
with thepageIndex
variable.pageIndex
won't be altered.