I am developing a Flutter application and I want to dynamically adjust the height of the ListView.
I want the list to have a maximum height of 200
. If the height is more than that, user will have to scroll to reach the bottom. If height is below 200
, it will take only as much space as needed.
Preview of application: Screenshot. As you can see Restaurants nearby
is pushed to the very bottom of the page. I want the ListView to only take height of 200
or less so that the content below isn't pushed to the bottom.
Here is my current code:
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Restaurants nearby',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Enter restaurant manually'),
onPressed: () {
print('Button pressed');
},
),
],
),
Flexible(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.cyan,
),
title: Text('Test restaurant'),
subtitle: Text('80m'),
);
},
itemCount: 15,
),
),
Text(
'Restaurants nearby',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
],
),
);
}
You have to change Flexible to ConstrainedBox and add shrinkWrap: true to your ListView . The height is not dynamic in this case. If ListView has height less than 200 , then SizedBox will still take height of 200 .
Flutter ListView – Set or Limit Height To limit the height of ListView, wrap the ListView with a Container widget and set the height of the Container to the required height.
Answer To multiply just add a factor of your choice (can also be dynamic): height * 1.5 or height * dynamicFactor . Edit (as the OP figured out correctly): The IntrinsicWidth class can be used to snap to a multiple of the stepWidth or stepHeight with their respective properties to be set to a non-null value.
Dynamic Display using ListView. We can use ListView. builder(..) in Stateless Widgets if we want to display dynamic (different) contents every time the widget is loaded within an application. With Stateful widgets, it can change the contents of the screen dynamically.
You are using Flexible
widget, that's why your ListView
expands. You have to change Flexible
to ConstrainedBox
and add shrinkWrap: true
to your ListView
.
ConstrainedBox(
constraints: BoxConstraints(maxHeight: 200, minHeight: 56.0),
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: CircleAvatar(
backgroundColor: Colors.cyan,
),
title: Text('Test restaurant'),
subtitle: Text('80m'),
);
},
itemCount: 15,
),
),
More info here: https://api.flutter.dev/flutter/widgets/ConstrainedBox-class.html
You can use LimitedBox
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(...),
Divider(),
Row(...),
LimitedBox(
maxHeight: 200.0,
child: ListView.builder(...),
),
Text(...),
],
),
Recommended solution in case when the incoming constraints are unbounded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With