How to get the Width and Height of the Screen in Flutter?

This Article is posted by seven.srikanth at 8/16/2019 3:18:14 PM




You can get the width and height of the Screen through MediaQuery.Size property.

Here is the code snippet from which you can get the width and height of the screen of the phone.

final sizeX = MediaQuery.of(context).size.width;
final sizeY = MediaQuery.of(context).size.height;
 
Here is a sample example of how to use it in a program.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Rows and Columns'),
),
body:
Home(),
),
);
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
final sizeX = MediaQuery.of(context).size.width;
final sizeY = MediaQuery.of(context).size.height;
return Container(
color: Colors.red,
width: sizeX,
height: sizeY,
child:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('Width: $sizeX' ),
Text('HeiGht: $sizeY' ),
],
)
);
}
}

Hope this helps.
Thanks,
Srikanth

Tags: Container; Mediaquery; height; width; screen; size








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com