user2353285
user2353285

Reputation: 85

programmatically prevent scroll listview flutter

I have nested ListView in a ListView. So I want to disable scroll for child ListView programmatically. How to do it. Thanks,

Upvotes: 3

Views: 2687

Answers (3)

Alex Collette
Alex Collette

Reputation: 1784

If you want to set it programatically, use a setState() to update the physics type like this:

ScrollPhysics physics = AlwaysScrollableScrollPhysics();


return Stack(
  children: [
    ListView(
      physics: physics,
    ),
    RaisedButton(
      child: Text("Disable scrolling"),
      onPressed: () {
        setState((){
          if(physics is AlwaysScrollableScrollPhysics){
            physics = NeverScrollableScrollPhysics();
          } else {
            physics = AlwaysScrollableScrollPhysics();
          }
        });
      },
    ),
  ]
);

Upvotes: 3

Anil Kumar
Anil Kumar

Reputation: 1984

You need to set this "physics" property value "NeverScrollableScrollPhysics" inside the nested listview.

example:

ListView.builder(
physics: NeverScrollableScrollPhysics(),
 itemBuilder: () {}
  ),

Upvotes: 0

Marcel Dz
Marcel Dz

Reputation: 2714

You can prevent it by using physics: NeverScrollableScrollPhysics() for example

Expanded(
                child: ListView.builder(
                    physics: NeverScrollableScrollPhysics(), <-----
                    scrollDirection: Axis.horizontal,
                    itemCount: status == false ? list.length : 5,
                    itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
                        value: list[i], child: ListItem()),
                  ),

Upvotes: 5

Related Questions