size property
The size of the media in logical pixels (e.g, the size of the screen).
Logical pixels are roughly the same visual size across devices. Physical pixels are the size of the actual hardware pixels on the device. The number of physical pixels per logical pixel is described by the devicePixelRatio.
Prefer using MediaQuery.sizeOf over MediaQuery.of.size
to get the
size, since the former will only notify of changes in size, while the
latter will notify for all MediaQueryData changes.
For widgets drawn in an Overlay, do not assume that the size of the Overlay is the size of the MediaQuery's size. Nested overlays can have different sizes.
Troubleshooting
It is considered bad practice to cache and later use the size returned by
MediaQuery.sizeOf(context)
. It will make the application non-responsive
and might lead to unexpected behaviors.
For instance, during startup, especially in release mode, the first returned size might be Size.zero. The size will be updated when the native platform reports the actual resolution. Using MediaQuery.sizeOf will ensure that when the size changes, any widgets depending on the size are automatically rebuilt.
See the article on Creating responsive and adaptive apps for an introduction.
See also:
- FlutterView.physicalSize, which returns the size of the view in physical pixels.
- FlutterView.display, which returns reports display information like size, and refresh rate.
- MediaQuery.sizeOf, a method to find and depend on the size defined for a BuildContext.
Implementation
final Size size;