Skip to main content

Command Palette

Search for a command to run...

MediaQuery widget and Attributes

Published
2 min read
MediaQuery widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The MediaQuery widget in Flutter is a powerful tool for building responsive layouts. It provides information about the current media, such as the size of the screen and device orientation. This allows developers to create UIs that adapt to various screen sizes and orientations.

Attributes:

  1. data (MediaQueryData?):

    • The media query data to use. If not provided, the MediaQuery widget will automatically obtain the MediaQueryData from the nearest ancestor MediaQuery widget.
  2. child (Widget):

    • The widget to which the media query data should be made available. Typically, the child widget consumes the MediaQuery data to adjust its layout or behavior accordingly.

Example:

import 'package:flutter/material.dart';

class MediaQueryExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MediaQuery Widget Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Screen Width: ${MediaQuery.of(context).size.width}',
              style: TextStyle(fontSize: 18),
            ),
            Text(
              'Screen Height: ${MediaQuery.of(context).size.height}',
              style: TextStyle(fontSize: 18),
            ),
            SizedBox(height: 20),
            Text(
              'Orientation: ${MediaQuery.of(context).orientation}',
              style: TextStyle(fontSize: 18),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: MediaQueryExample(),
  ));
}

Explanation:

  • The MediaQuery widget is used to access information about the current media, such as screen size and orientation.

  • In the example, MediaQuery.of(context) retrieves the MediaQueryData for the current context.

  • The size property of MediaQueryData provides the size of the screen, and width and height are accessed to display the screen width and height.

  • The orientation property of MediaQueryData provides the orientation of the screen, whether it's portrait or landscape.

  • This information can be used to create responsive layouts that adapt to different screen sizes and orientations.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"