Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter PageView: Disable left or right side scrolling

Tags:

flutter

dart

I have a PageView, how can I disable the left or right scrolling. I know by using NeverScrollableScrollPhysics we can disable the scrolling but how to disable the scrolling in one direction.

like image 870
shivam Avatar asked Apr 03 '19 19:04

shivam


People also ask

How do I stop scrolling in PageView flutter?

You may come across cases like where you need to disable swipe or scrolling for your Scrolling widgets (ListView, PageView etc). You can simply achieve this by setting physics to NeverScrollableScrollPhysics. Hope this is useful to you.

How do I turn off scrolling in flutter?

How do I stop list view scrolling in flutter? Flutter ListView – Never Scrollable You can make the ListView widget never scrollable by setting physics property to NeverScrollableScrollPhysics().

What is the default scroll of PageView?

By default, the PageView comes with a horizontal scroll for the pages. You can change the scroll direction using the Axis property.


2 Answers

You can create your own ScrollPhysics to allow only go to the right:

            class CustomScrollPhysics extends ScrollPhysics {
              CustomScrollPhysics({ScrollPhysics parent}) : super(parent: parent);

              bool isGoingLeft = false;

              @override
              CustomScrollPhysics applyTo(ScrollPhysics ancestor) {
                return CustomScrollPhysics(parent: buildParent(ancestor));
              }

              @override
              double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
                isGoingLeft = offset.sign < 0;
                return offset;
              }

              @override
              double applyBoundaryConditions(ScrollMetrics position, double value) {
                //print("applyBoundaryConditions");
                assert(() {
                  if (value == position.pixels) {
                    throw FlutterError(
                        '$runtimeType.applyBoundaryConditions() was called redundantly.\n'
                        'The proposed new position, $value, is exactly equal to the current position of the '
                        'given ${position.runtimeType}, ${position.pixels}.\n'
                        'The applyBoundaryConditions method should only be called when the value is '
                        'going to actually change the pixels, otherwise it is redundant.\n'
                        'The physics object in question was:\n'
                        '  $this\n'
                        'The position object in question was:\n'
                        '  $position\n');
                  }
                  return true;
                }());
                if (value < position.pixels && position.pixels <= position.minScrollExtent)
                  return value - position.pixels;
                if (position.maxScrollExtent <= position.pixels && position.pixels < value)
                  // overscroll
                  return value - position.pixels;
                if (value < position.minScrollExtent &&
                    position.minScrollExtent < position.pixels) // hit top edge

                  return value - position.minScrollExtent;

                if (position.pixels < position.maxScrollExtent &&
                    position.maxScrollExtent < value) // hit bottom edge
                  return value - position.maxScrollExtent;

                if (!isGoingLeft) {
                  return value - position.pixels;
                }
                return 0.0;
              }
            }

Usage:

            @override
              Widget build(BuildContext context) {
                return Scaffold(
                    appBar: AppBar(),
                    body: PageView.builder(
                      itemCount: 4,
                      physics: CustomScrollPhysics(),
                      itemBuilder: (context, index) => Center(
                            child: Text("Item $index"),
                          ),
                    ));
              }
like image 137
diegoveloper Avatar answered Sep 19 '22 05:09

diegoveloper


You can also use the horizontal_blocked_scroll_physics library which I recently wrote that will allow you to block the left and right movements.

like image 33
robertohuertasm Avatar answered Sep 21 '22 05:09

robertohuertasm