How to set Minimum and Maximum Height or Width of Widget

Are you trying to set minimum or maximum height or width of Container() widget in a Flutter, then use 'constraints' attribute and apply BoxConstraints() on it like below.

Container(
   constraints: BoxConstraints( 
      minHeight: 500, //minimum height
      minWidth: 300, // minimum width
      
      maxHeight: MediaQuery.of(context).size.height,
      //maximum height set to 100% of vertical height
      
      maxWidth: MediaQuery.of(context).size.width,
      //maximum width set to 100% of width
   ),
)

Alternatively, you can use ConstrainedBox() like below. But I recommend the above one because it needs less code to apply minimum and maximum height or width. In case there is no attributes like constraints in the widget then, you should use this method.

ConstrainedBox(
    constraints: BoxConstraints(
      maxHeight: 300, 
      minHeight: 200,
      maxWidth: 500,
      minWidth: 200
    ),
    child:Container(
       // contents here
    )
)

In this way, you can set minimum and maximum height or width on Container() or any other widget.

No any Comments on this Article


Please Wait...