Flutter – Managing the MediaQuery Object
During the process of developing an app for both phones and tablets, it is standard practice to have different UI layouts for different screen sizes for a better user experience. If the user has a preference set for different font sizes or wants to curtail animations. This is where MediaQuery comes into action, you can get information about the current device size, as well as user preferences, and design your layout accordingly. MediaQuery provides a higher-level view of the current app’s screen size and can also give more detailed information about the device and its layout preferences. In practice, MediaQuery is always there. It can simply be accessed by calling MediaQuery.of in the build method.
From there you can look up all sorts of interesting information about the device you’re running on, like the size of the screen, and build your layout accordingly. MediaQuery can also be used to check the current device’s orientation or can be used to check if the user has modified the default font size. It can also be used to determine if parts of the screen are obscured by a system UI, similar to a safe area widget.
Using mediaQuery.of automatically causes the widgets to rebuild themselves according to the current device sizes and layout preferences every time they change.
Example 2: Getting device orientation and rebuilding accordingly.
Note: You can change the orientation by putting these code before the runApp() in your main.dart file