Home > OS >  How to get real device pixels
How to get real device pixels

Time:10-24

i have weird results that i want to understand .

1- i have image in my device gallery .

2- I decided to find out the height of an image using image details

3- the given height was 1920

4- Out of curiosity, I wrote the following code

Container(
color:Colors.yellow,
width:300,
height:1920, // i know it is big height but it is the same image height 
)

5- the output of height was completely bigger than image height

Question : why it was bigger ? Isn't it the same height?

CodePudding user response:

Because the flutter uses logical pixel unit to render ui, but you read the image sizes in pixel unit.

You can read more about logical pixel in flutter documents here: https://api.flutter.dev/flutter/dart-ui/FlutterView/devicePixelRatio.html

To fix that, you need to divide pixel size by devicePixelRatio like this:

Container(
    color:Colors.yellow,
    width:300,
    height:1920/MediaQuery.of(context).devicePixelRatio, 
)
  • Related