Flutter: How to set the minimum height to Widget?
This is a very common thing that comes in day-to-day development, but I found in Flutter there is no such documents available that explain this concept in short. So, now I will try to explain in short as possible.
First, required BoxConstraints
which provides the boundaries to the Widget, it has params like:
this.minWidth = 0.0,
this.maxWidth = double.infinity,
this.minHeight = 0.0,
this.maxHeight = double.infinity,
Above are the default params which have for every widget, here we need to modify two params which will do our job
- minWidth — The minimum width that satisfies the constraints
- maxHeight — The minimum height that satisfies the constraints.
Container(
color: Colors.blueAccent,
constraints: BoxConstraints(
minHeight: 100, minWidth: double.infinity, maxHeight: 400),
child: ListView(
shrinkWrap: true,
children: <Widget>[
...List.generate(
10, // Replace this with 1, 2 to see min height works.
(index) => Text(
'Sample Test: ${index}',
style: TextStyle(fontSize: 60, color: Colors.black),
),
),
],
),
),
Output for Min Height for Single Item:
Output for Min Height for 10 Items:
Note: This will show widgets as per mentioned max-height. The above image clearly tells you that it shows only half Texts not all of them, as max height is mentioned.
Youtube: