pksasso
pksasso

Reputation: 429

Disable ListView Scroll

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??

Upvotes: 15

Views: 11565

Answers (3)

Albert Lardizabal
Albert Lardizabal

Reputation: 6856

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.

Upvotes: 9

DAG
DAG

Reputation: 6994

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>[/**/],
        ),
    )

Upvotes: 17

spenster
spenster

Reputation: 538

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(),

Upvotes: 12

Related Questions