flutterscrollflutter-pageview

How to disable scroll in PageView in a particular direction in flutter


I'm new to flutter and am learning about Pageview. In my app I have made an onboarding screen. Is there a way to disable scroll in a particular direction? What I'm trying to achieve is that the user should not be able to scroll/swipe left when they are on the first page and should not be able to scroll/swipe right when they are on the last page. I know you can disable scroll physics by using NeverScrollableScrollPhysics(), but is there a way to give it a direction?

Code:

PageView(
  onPageChanged: (newValue) {
    //Update the page
  },
  physics: activePage == 0 ||
          activePage == pages.length - 1
      ? const NeverScrollableScrollPhysics()
      : const PageScrollPhysics(),
  controller: pageController,
  children: [
    for (var page in pages) PageWidget(page: page),
  ],
)

Solution

  • Found the answer. Simply set physics of the PageView to ClampingScrollPhysics().