Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter screens sizes

Container(height:50, width:50)

How come I define a container of 50px * 50px (with some text inside), but the container is quite smaller on SAMSUNG SCREEN than on HUAWEI SCREEN. There even is an overflow.

I would understand if this happened while using Media Querry.

Related Question

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone explain, please?

>Solution :

Different devices have different resolutions and dpi, which causes your widgets to look smaller/bigger on different devices

in Container(height:50, width:50) 50 is not pixel unit, it is dp

px = dp * (dpi / 160)

For example, with a device with a dpi of 320, with 10 dp we have: 10 * (320/160) = 20 px, 1 dp is equivalent to 2 px.

if you want convert pixel to device size unit, try:

 double convertFromPixel(double pixel){
    var size = MediaQuery.of(context).devicePixelRatio; //3.5
    var value = pixel / size; //3.5dp = 1px
    return value; 
  }

and use:

Container(
    height: convertFromPixel(50), //50 pixel, height will auto size 
    width: convertFromPixel(50),
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading