Home > front end >  how to get device pixel ratio using flutter
how to get device pixel ratio using flutter

Time:12-27

as you might know there are two types of size in flutter app (physical,logical). for margin and padding if we want the app to be responsive we must get context size (width,height). for a reason I don't want to use MediaQuery.of(context).size so I'm using

Size screenSize = WidgetsBinding.instance.window.physicalSize;
    double width = screenSize.width;
    double height = screenSize.height;

in such case the size is physical and can't be used like before for padding and margin with edgeinsets. what I'm asking is a way to either change physical pixel size to logical without using context and MediaQuery or a way to use physical pixels to give responsive margin and padding.

thanks in advance.

I tried to get the ratio with testing results it differs between devices so we can't use static ratio. The ratio is not the ratio of height/width of physical size.

CodePudding user response:

You can get the screen size without context like this:

var size = MediaQueryData.fromWindow(WidgetsBinding.instance.window).size;

now if you print this size, you can see it is equals to the size that you get from MediaQuery with context.

  • Related