如何在向前插入项目时保持 Flutter PageView 的状态?

发布于 2025-01-16 12:41:14 字数 275 浏览 4 评论 0原文

我从后端获取一些视频数据并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2025-01-23 12:41:16
  • 尝试在变量中维护 pageIndex ,并在 initState 方法中尝试调用另一个方法,例如 _loadData() 来获取数据。
  • 在需要时使用这个 _loadData() 从后端加载数据。
  • 仅当 pageController 更改时才更新 pageIndex 的状态。
  • 使用 pageIndex 变量提供 PageView 小部件的 initialIndex
  • 这样,在重新加载或从后端加载任何新数据时,仅加载数据,并且 pageIndex 的状态不会改变。
  • Try to maintain the pageIndex in a variable and in the initState method try calling another method like _loadData() which get's the data.
  • Use this _loadData() to load the data from backend whenever needed.
  • Update the state of pageIndex only when the pageController is changed.
  • Feed the PageView widget's initialIndex with the pageIndex variable.
  • This way on reload or loading any new data from backend only the data is loaded and the state of pageIndex won't be altered.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文