简体   繁体   中英

Flutter Page View how to not always start at the initialPage?

It may sound complicated but I try to explain what Im trying to do

I have a Page View that contains different pages and also another page view like this:

PageView(
  controller: pageController,
  physics: NeverScrollableScrollPhysics(),
  children: [FirstPage(), 
             SecondPage(), 
             SmallPageView(), 
             ThirdPage()])

While this works as intended there is one problem: The SmallPageView() has 4 Pages. When I went through its pages and I am at ThirdPage() and press the Back Button, which calls:

pageController.previousPage(duration: Duration(milliseconds: 400), curve: Curves.ease);

I am in SmallPageView() at page 1 again. Which makes sense since it is the initialPage and the previousPage of the pageController . However when I am at ThirdPage() and press the back button I would like to go to the last page of the SmallPageView() which is obviously more intuitive

I tried doing things like

pageController.previousPage(duration: Duration(milliseconds: 400), curve: Curves.ease);
smallPageViewControllerHouse.jumpToPage(4);

Which doesnt work and I suppose would look ugly.

I also cant assign a variable to initialPage of the controller to change it dynamically. Is there a way to 'remember' where a page view was instead of starting at the initial page everytime time?

And of course you could argue to just resolve the SmallPageView() and put its 4 pages into the PageView() instead of a nested page view. But this is a design solution and to achieve the look I am going for there is no other way (as far as I know atm) than doing it with a nested page view

try to use IndexedStack, pageview inside indexedstack will maintain it's state

IndexedStack(
  index: _currentIndex,
  children: [FirstPage(), 
             SecondPage(), 
             SmallPageView(), 
             ThirdPage()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM