Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: ListView maximum height

I can set a minimum height doing something like this: ListView( shrinkWrap: true, children: listItems.toList(), );

But how could I have a maximum height to have for example max 3 elements displayed? (to see the rest I'd have to scroll)

like image 544
Dani Avatar asked Nov 03 '25 21:11

Dani


1 Answers

You can wrap your ListView with a SizedBox, like this :

SizedBox(
    height: 300,
        child: ListView(
            children: <Widget>[
                SizedBox(height: 100, child: Placeholder()),
                SizedBox(height: 100, child: Placeholder()),
                SizedBox(height: 100, child: Placeholder()),
                SizedBox(height: 100, child: Placeholder()),
            ],
          ),),

Edit : If you want the container to shrink when there is less than 3 items, set the maxHeight constraint for the container and shrinkWrap: true for the ListView :

Container(
  decoration: BoxDecoration(border: Border.all(width: 2.0, color: Colors.red)),
  constraints: BoxConstraints(maxHeight: 300),
  child: ListView(
    shrinkWrap: true,
    children: <Widget>[
      SizedBox(height: 100, child: Placeholder()),
      SizedBox(height: 100, child: Placeholder()),
    ],
  )
),
like image 114
MickaelHrndz Avatar answered Nov 06 '25 21:11

MickaelHrndz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!