Felipe Xavier
Felipe Xavier

Reputation: 41

How to dynamically disable vertical swipe in PageView

I would like to find a way to update the physics param to disable the swipe action in a parent PageView from a child widget.

I am using riverpod for updating the state in a child widget when it builds to know when I should pass NeverScrollableScrollPhysics to the physics param in a parent widget. But the thing is that this approach is causing my child widget to rebuild recursively, because this is making the PageView rebuild to update the physics param. So I really don't know what to do here.

I have this parent Widget that builds the PageView:

Widget build(BuildContext context) {
  final _navBar = useProvider(bottomNavigationBarProvider);
  return PageView(
    physics: navBar.isSwipeBlocked ? const NeverScrollableScrollPhysics() : null,
    controller: pageController,
    onPageChanged: onPageChanged,
    children: [
      Beamer(
        key: const Key('feed-tab'),
        routerDelegate: BeamerDelegate(locationBuilder: (state) => FeedLocation(state)),
      ), 
    ]
  )
}  

And the child Widget that updates the state variable:

Widget build(BuildContext context) {
  final _navBar = useProvider(bottomNavigationBarProvider.notifier);
  useEffect(() {
    Future.microtask(() => _navBar.blockSwipe());
  }, []);
  return Container(...);
}

So when FeedLocation loads, it updates _navBar in an attempt for disabling the scroll behavior. But as I mentioned, this causes the parent to rebuild and FeedLocation to build again and then the recursive state..

The idea was to be able to go to FeedLocation, disable the scroll, then when go back, enable it again, but I don't see a solution for that.

I think I did already what this guy suggested https://github.com/flutter/flutter/issues/37510#issuecomment-738051469 using Riverpod

And I guess I am a similar situation as this guys from the same thread https://github.com/flutter/flutter/issues/37510#issuecomment-864416592

Is anybody able to see a solution or what I am doing wrong?

Upvotes: 1

Views: 1591

Answers (1)

MindStudio
MindStudio

Reputation: 786

You should replace null as the secondary ScrollPhysics with PageScrollPhysics() and make sure to add setState(() {}); when you update the isSwipeBlocked variable.

Upvotes: 1

Related Questions