Home > Back-end >  flutter: Is it possible to use conditional statements in pageview?
flutter: Is it possible to use conditional statements in pageview?

Time:11-08

I want to create a function that automatically moves to the next page when a certain condition is met using 'pageview'. For example, if a=0 becomes a=1, it should automatically advance to the next page. Instead of going to the next page by clicking a button in the pageview, is it possible to automatically go to the next page using a conditional statement?

             Expanded(

            child:
                
            PageView.builder(
              physics: NeverScrollableScrollPhysics(),
              controller: _questionController.pageController,
              onPageChanged: _questionController.updateTheQnNum,
              itemCount: _questionController.questions.length,
              itemBuilder: (context, index) => QuestionCard(
                question: _questionController.questions[index],
                myanswer: testtext,
              ),
            )
          ),

This is part of my code. When a=1, it should advance to the next page, otherwise it should keep the current page.

CodePudding user response:

There are two methods that can help you achieve it.

animateToPage

_questionController.pageController.animateToPage(pageIndex);

or

jumpToPage

_questionController.pageController.jumpToPage(pageIndex);

As the name suggests animateToPage will animate the transition from Page A to Page B where jumpToPage won't animate.

Make sure pageIndex is within the range.

  • Related