flutterdartflutter-layoutflutter-pageview

how to go back to previous page when refresh the screen in pageview flutter


I want to keep the current page, when i refersh the screen or navigating back in page view here is the code , actually i want display the particular index screen programmatically with out clicking the button. Some times iam getting controller.hasClients getting empty. If page view has 3 screens , if iam on 2ndscreen when i refresh the second screen should display second screen, instead it always display 1st screen . Iam using page controller to display , but not able to instantiate page controller becacuse i want to access from top widgets of page view. Please help

      Container(
          // width: 370,
          height: MediaQuery.of(context).size.height * 0.685,
          child: Stack(children: <Widget>[
            _pages(),

            Stack(
              children: <Widget>[
                Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        for (int i = 0; i < 3; i++)
                          (i == currentPage
                              ? circleBar(true)
                              : circleBar(false))
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ]),
        ),

           Widget _pages() {
List _pagesList = [
  FirstScreen(
   
  ),
  SecondScreen(
   
  ),
  ThirdScreen(
      ),
];


return PageView.builder(
  controller: _pageController,
  itemCount: _pagesList.length,
 // restorationId: currentPage.toString(),
  itemBuilder: (BuildContext context, int index) {
   // _pageController=PageController(initialPage: currentPage);
    return _pagesList[index];
  },
  onPageChanged: (int page) {
    setState(() {
      currentPage = page;
      selected = analyticsList[currentPage].anlyticName;
      isIndexChanged = true;
      page;
    });
  },
);

}


Solution

  • first saving the current page and instantiating page controller in pages() it worked for me

    i just instantiated the page controller in pages() in my code as if declare on the top it is not getting read every refresh so i declared where the pageview get build .

    Widget _pages() {
    
       _pageController= PageController(initialPage: currentPage, keepPage: true);
        List _pagesList = [
          FirstScreen(
            
          ),
         SecondScreen(
           
          ),
          ThirdScreen(
              ),
        ];
    
    
        return PageView.builder(
          controller: _pageController,
          itemCount: _pagesList.length,
         // restorationId: currentPage.toString(),
          itemBuilder: (BuildContext context, int index) {
    
            return _pagesList[index];
          },
          onPageChanged: (int page) {
            setState(() {
              currentPage = page;
              selected = analyticsList[currentPage].anlyticName;
              isIndexChanged = true;
    
              page;
            });
          },
        );
    
    
    
      }