Disable ListView Scroll

6,690

Solution 1

I think the best way to do this is to use a SingleChildScrollView:

This widget is useful when you have a single box that will normally be entirely visible, for example a clock face in a time picker, but you need to make sure it can be scrolled if the container gets too small in one axis (the scroll direction).

And instead of using a ListView just use a Column and place it inside of the SingleChildScrollView:

    SingleChildScrollView(
        child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[/**/],
        ),
    )

Or if you need to use ListView for some reason, you can use shrinkWrap with NeverScrollableScrollPhysics:

    SingleChildScrollView(
        child: ListView(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            children: <Widget>[/**/],
        ),
    )

Solution 2

You can set the scroll physics property on the ListView, which will disable scrolling:

shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),

You can also switch between enabling or disabling scrolling at runtime if you have to via some condition like so:

shrinkWrap: true,
physics: disableScrolling ? NeverScrollableScrollPhysics() : 
         AlwaysScrollableScrollPhysics(),

Solution 3

Unless that scroll view needs to use the parent's PrimaryScrollController, setting the ListView's primary property to false will result in the scroll view scrolling only if there is enough content.

property documentation:

Whether this is the primary scroll view associated with the parent PrimaryScrollController.

When this is true, the scroll view is scrollable even if it does not have sufficient content to actually scroll. Otherwise, by default the user can only scroll the view if it has sufficient content. See physics.

On iOS, this also identifies the scroll view that will scroll to top in response to a tap in the status bar.

Defaults to true when scrollDirection is Axis.vertical and controller is null.

Share:
6,690
pksasso
Author by

pksasso

Updated on December 12, 2022

Comments

  • pksasso
    pksasso over 1 year

    Is possible to disable ListView Scroll if page don't overflow the screen size?

    I always use ListView to avoid overflow the screen, but when the content of the page is smaller than screen I can see the end scroll animation, how can i disable the scroll in this cases, and able to scroll if the screen size is small??

  • pksasso
    pksasso almost 5 years
    Perfect, the singleChildScrollView works exactly like i want.
  • Ber
    Ber over 3 years
    I like the NeverScrollableScrollPhysics option best to diasable scolling. Also works perfectly with ListWheelScrollView