Search code examples
flutterdartgesture-recognition

How to navigate through PageView in Flutter?


I'm trying to make widgets behind a PageView clickable by wrapping it around a GestureDetector, but it doesn't work.

Is there another way I can do this?

My code:

 new GestureDetector(
        behavior: HitTestBehavior.translucent,
        child: new PageView(
          controller: _pageController,
          children: _buildForegroundPages(),
        ),
      ),

Solution

  • What you need is to wrap each page inside your PageView in a GestureDetector, like this.

      PageView(
            children: [
              //Page1
    
              GestureDetector(
                onTap: () {
                  print("Click page1");
    
                  Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => NewPage()
                  ));
    
                },
                child: Container(
                  color: Colors.red,
                  child: Center(
                    child: Text("text 1"),
                  ),
                ),
              ),
    
              //Page2
              GestureDetector(
                  onTap: () {
                    print("Click page2");
                  },
                  child: Container(
                    color: Colors.blue,
                    child: Center(
                      child: Text("text 1"),
                    ),
                  )),
    
              //Page3
              GestureDetector(
                  onTap: () {
                    print("Click page3");
                  },
                  child: Container(
                    color: Colors.green,
                    child: Center(
                      child: Text("text 1"),
                    ),
                  )),
            ],
          );