1

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!!

Alvin
  • 11
  • 1
  • I'd suggest you use a state-management library like Riverpod, GetX, etc. When the page is changed, save the current video's data (duration played) alongside the current page's index. When the user goes back to the previous page, load the video and update its start position to the previously saved value. For a smooth transition, Implement a caching layer for your video player. – Marcos Maliki Mar 22 '22 at 14:00
  • This is a nice direction, I can try it, thanks for your suggestion! – Alvin Mar 23 '22 at 06:24

1 Answers1

0
  • 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.
  • Thanks for your patience, but I need to rebuild `itemBuilder` for PageView after I load video data so that I can page forward to show new video.Under this condition, the page index must be changed.For example, the current page index is 2, then I load and insert 3 new video data forward, the current page index change to (5 =2 + 3) to keep showing current page. – Alvin Mar 23 '22 at 06:42
  • Not sure if I understood the use case correctly, but I did understand correctly, try setting the state of `pageIndex` in `_loadData()` method with following equation: `pageIndex = pageIndex + offest` – Prudhvik Chirunomula Mar 24 '22 at 07:00
  • 1
    I probably understand what you mean that I need to change the index in page widget, but I call `jumpToPage` after change index now. Thanks!!! – Alvin Mar 25 '22 at 06:14