How to get Screen size in flutter?

This Article is posted by seven.srikanth at 11/14/2022 4:31:20 PM



You can get the size of the screen in flutter using MediaQuery as below.

var screenSize = MediaQuery.of(context).size;
Here is an example of how to use that in your program.

import 'package:flutter/material.dart';
void main(List<String> args) {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @ override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: MyContainer(),
      ),
    );
  }
}
class MyContainer extends StatelessWidget {
  const MyContainer({
    super.key,
  });
  @ override
  Widget build(BuildContext context) {
    var screenSize = MediaQuery.of(context).size;
    var screenOrientation = MediaQuery.of(context).orientation;
   
    return Center(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 100),
        child: Padding(
          padding: const EdgeInsets.all(28.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'ScreenSize - $screenSize ',
                style: const TextStyle(fontSize: 18.0),
              ),
              const SizedBox(
                height: 20,
              ),
              Text(
                'Screen Width - ${screenSize.width}',
                style: const TextStyle(fontSize: 18.0),
              ),
              const SizedBox(
                height: 20,
              ),
              Text(
                'Screen height - ${screenSize.height}',
                style: const TextStyle(fontSize: 18.0),
              ),
              const SizedBox(
                height: 20,
              ),
              Text(
                'Screen Orientation - $screenOrientation ',
                style: const TextStyle(fontSize: 18.0),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Thanks,
Srikanth

Tags: Screensize in flutter








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