Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How flutter calculates screen width

Tags:

flutter

I have a simple flutter setup to get the width of the screen on device with resolution 2960*1440.

However, flutter returns a screen width of 411 when I run the application on resolution 2960*1440. How does flutter calculate width of a device?

class page extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width.toString();

    return Container(
      child: Text("Height : $height and Width : $width"),

    );
  }
}
like image 890
cool_stuff_coming Avatar asked Apr 25 '26 23:04

cool_stuff_coming


1 Answers

flutter depends on the MediaQuery method to calculate screen size :

  MediaQuery.of(context).size.width ;

as per the documentation it returns the number of the "logical pixels" of your screen which each one of them represent several physical pixels by a factor that differs from a device to another, and if you want the actual number of pixels you can use :

  MediaQuery.of(context).size.width * MediaQuery.of(context).devicePixelRatio ;

and for the actual pixels of your screen height:

  MediaQuery.of(context).size.height * MediaQuery.of(context).devicePixelRatio ;
like image 148
Mazin Ibrahim Avatar answered Apr 27 '26 14:04

Mazin Ibrahim