Reputation: 85
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
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
Reputation: 1984
You need to set this "physics" property value "NeverScrollableScrollPhysics" inside the nested listview.
example:
ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemBuilder: () {}
),
Upvotes: 0
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