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)
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()),
],
)
),
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