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),
],
)
2
Answers
You can achieve this behaviour like this
set physics as
NeverScrollableScrollPhysics()
thenyou can have a
next
button, where you can usepageView.animateToPage()
function to animate to next pageThis will give you the feel of swiping and can achieve this behaviour.
Here by using this you can only move forward not backward in your
pageView
If you have any doubts let me know
To get that kind of behaviour you can give
controller
to pageview and get swipe direction.Then you can block the scroll according to you.
Refer the link below
https://stackoverflow.com/a/63188690/15438326