Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter dynamic height of ListView

Tags:

flutter

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,
          ),
        ),
      ],
    ),
  );
}
like image 515
Bill Avatar asked Jun 22 '19 16:06

Bill


People also ask

How do I give dynamic height to ListView in flutter?

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 .

How to set height in ListView in Flutter?

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.

How do you get dynamic height in flutter?

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.

What is list dynamic in Flutter?

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.


2 Answers

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

like image 71
diegoveloper Avatar answered Sep 18 '22 01:09

diegoveloper


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

like image 42
Andrey Turkovsky Avatar answered Sep 17 '22 01:09

Andrey Turkovsky