9.4 C
New York
Saturday, December 2, 2023

Flutter: Get Device Screen Width and Height

In Flutter, you can get the device screen width by using:

MediaQuery.of(context).size.width;

To get the screen height, use:Advertisements

MediaQuery.of(context).size.height;

Example

Screenshot:

Full code in main.dart:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Hide the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Kindacode.com'),
      ),
      body: Center(
        child: Text(
          "Width: $width, height: $height",
          style: const TextStyle(fontSize: 20, color: Colors.purple),
        ),
      ),
    );
  }
}

Hope this helps. Futher reading:

AdvertisementsYou can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Related Articles

Latest Articles