1 / 10

How to Determine Screen Height and Width in Flutter

Here in this article, you will see how you can determine the height and width of the screen using MediaQuery in an easy way. Read the article for more information.

Ruben8
Download Presentation

How to Determine Screen Height and Width in Flutter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to Determine Screen Height and Width in Flutter? Sometimes, you may need to know the size and position of a widget rendered on the screen programmatically. For example, you will need to control a child’s widget based on the parent’s size and location. So, we’ll see how to determine screen height and width in the Flutter app. In this article, Flutter Agency will teach you how to access the device screen width and height in the Flutter app in the simplest possible method.

  2. You want to have distinct UI layouts for different screen sizes if you’re building an app for both phones and tablets. If the customer prefers a much larger text size or wants to minimize animations, you can find yourself in a similar scenario. You can use MediaQuery to measure the size of the device you’re using and adjust your layout accordingly. MediaQuerygives you a more complete picture of your device screen size and might provide you more information about user layout preferences.

  3. You may get the width or height of the screen by using MediaQuery with the current context of your widget. MediaQuery returns a high-level view of the current app’s screen size, as well as more comprehensive information about the device and its layout preferences. It’s as simple as invoking MediaQuery.of in the build method to get to it. double width = MediaQuery.of(context).size.width; double height = MediaQuery.of(context).size.height;

  4. You need a MaterialApp or a WidgetsApp around your widget. They provide MediaQuery. When you call .of(context) flutter will always look up the widget tree to find the widget. If you don’t do this, you may get “No MediaQuery widget found” error. See more details at: ’No MediaQuery widget found’ Error in Flutter. Wrap your Widget at the root with the MaterialApp() widget, as shown below: Widget build(BuildContext context) { return MaterialApp( home: Home() ); }

  5. Apply the percentage of Screen height and width to the size of child widgets. Container( height: (MediaQuery.of(context).size.height / 2), width: (MediaQuery.of(context).size.width / 2), color: Colors.green, child:const Center(child: Text("Media Query Container")), ), Let’s have an Example to Determine the Screen size using the MediaQuery class.

  6. class Home extends StatefulWidget { const Home({Key? key}) : super(key: key); @override _HomeStatecreateState() => _HomeState(); } class _HomeState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Media Query"), ),

  7. body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: (MediaQuery.of(context).size.height / 2), width: (MediaQuery.of(context).size.width / 2), color: Colors.green, child: const Center(child: Text("Media Query Container")), ) ], ), ), ); } }

  8. Output:

  9. Conclusion: So in this article, we will go through how to determine screen height and width in a Flutter app. This article will help you to make your flutter app responsive. We hope you enjoyed this article. Article Originally published at: https://flutteragency.com/determine-screen-height-width-flutter/

More Related