Liu Silong
Liu Silong

Reputation: 5532

Flutter how to detect PageView onTapUp

I used PageView to write a list in the vertical direction. Now when I slide the list up or down (my finger doesn't leave the screen), onPageChanged will execute. Is there a way to detect that the onPageChanged method will only be executed when the finger leaves the screen.Part of the code below:


    SizedBox(
            width: 200,
            height: 200,
            child: PageView(
                controller: pageController,
                scrollDirection: Axis.vertical,
                onPageChanged: (index) {
                  setState(() {
                    pageIndex = index;
                  });
                },
                children: _buildCardList()),
          )

like below:

Upvotes: 1

Views: 1049

Answers (2)

nick.tdr
nick.tdr

Reputation: 4933

Wrap your page view with a Notification listener like below:

  SizedBox(
            width: 100,
            height: 100,
            child: NotificationListener<ScrollNotification>(
              onNotification: (notification)
              {
                if(notification is ScrollEndNotification)
                  {

                    var pageIndex = pageController.page;
                  }

              },
              child: PageView(
                  controller: pageController,
                  scrollDirection: Axis.vertical,
                  onPageChanged: (index) {
                  },
                  children: _buildCardList()),
            ),
          )

Upvotes: 3

Murat Aslan
Murat Aslan

Reputation: 1580

you can do it like that;

   int currentPage;
    .
    .
    .
    onPageChanged(int page){
      if(currentPage != page){
        setState((){
          currentPage = page;
        });
      }
    }

Upvotes: -1

Related Questions