Posted in Dart Flutter

Flutter – How to set Minimum and Maximum Height or Width of Container() Widget

Flutter – How to set Minimum and Maximum Height or Width of Container() Widget Posted on May 12, 2020Leave a comment
I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

Are you trying to set minimum 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 attribute 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.

I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

Leave a Reply

Your email address will not be published. Required fields are marked *