Flutter: How to navigate in PageView with a RaisedButton or move pageViewController with button

2,147

Solution 1

You have to have this controller:


  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

After this you can define your PageView widget. Now the raised button can have this for controlling the

                        onPressed: () {
                          if (_pageController.hasClients) {
                            _pageController.animateToPage(
                              1,
                              duration: const Duration(milliseconds: 400),
                              curve: Curves.easeInOut,
                            );
                          }
                        }, 

You have to note the index of the page.

Solution 2

This Code you can use for your requirement,

I have written in such a way that it will replicate Tabbar

first it's button bar like tabs:

Container(
  width: MediaQuery.of(context).size.width * 0.9,
  height: MediaQuery.of(context).size.height * 0.1,
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
      RaisedButton(
        onTap: () {
          setState(() {
            select1 = true;
          });
          pageController.animateToPage(0,
              duration: Duration(milliseconds: 500),
              curve: Curves.bounceOut
          );
        },
        child: Container(
            width: MediaQuery.of(context).size.width * 0.29,
            height: MediaQuery.of(context).size.height * 0.04,
            color: colorWhite,
            child: Text("First"),
        ),
      ),
      RaisedButton(
        onTap: () {
          setState(() {
            select2 = true;
          });
          pageController.animateToPage(1,
            duration: Duration(milliseconds: 500),
            curve: Curves.bounceOut
          );
        },
        child: Container(
            width: MediaQuery.of(context).size.width * 0.29,
            height: MediaQuery.of(context).size.height * 0.04,
            color: colorWhite,
            child: Text("Second")
        ),
      ),
      RaisedButton(
        onTap: () {
          setState(() {
            select3 = true;
          });
          pageController.animateToPage(2,
            duration: Duration(milliseconds: 500),
            curve: Curves.bounceOut
          );
        },
        child: Container(
            width: MediaQuery.of(context).size.width * 0.29,
            height: MediaQuery.of(context).size.height * 0.04,
            color: colorWhite,
            child: Text("Third")
        ),
      ),
    ]
  )
),

and this is arrays of Pages:

Container(
  height: MediaQuery.of(context).size.height * 0.8,
  child: PageView(
    controller: pageController,
    onPageChanged: (index) {
      setState(() {
        pageChanged = index;
        if(pageChanged == 0)
          select1 = true;
        else if(pageChanged == 1)
          select2 = true;
        else if(pageChanged == 2)
          select3 = true;
      });
      print("Page Changed is: $pageChanged");
    },
    children: <Widget>[
      Container(child: FirstPage()),
      Container(child: SecondPage()),
      Container(child: ThirdPage()),
    ],
  ),
)
Share:
2,147
Himanshu Sharma
Author by

Himanshu Sharma

I am a Software Engineer, specializing in the mobile development domain and a focused computer science major currently pursuing my Bachelor. For the past 2 years, I have been developing mobile and web apps using Flutter SDK and Dart programming language. I am an open-source enthusiast and I enjoy learning new technologies. Additionally, I am an organizer of Flutter Chandigarh and Flutter India. In my spare time, I love writing Technical Articles and Mentor fellow developers at Hackathons. And I am proud that my articles have reached out to 123K+ developers and helped a lot of organizations to grow their products. If you want to talk about Flutter, Dart, Technical Writing, or anything else, feel free to drop me a message or contact me at [email protected].

Updated on December 18, 2022

Comments

  • Himanshu Sharma
    Himanshu Sharma over 1 year

    I want to create a section of Page View in which we can navigate between its children Pages on pressing a unique raised button.

    Each button will navigate to different page.

    How can I achieve this?