Google News
logo
Flutter - Interview Questions
What is a Flutter media query and how is it used?
In Flutter, a media query is a way to retrieve information about the device's display dimensions and other information related to the user's device. A media query can be used to build a responsive UI, which adapts to the different screen sizes, orientations, and other characteristics of a device.

To use a media query in Flutter, you can use the `MediaQuery` class provided by the Flutter framework. This class provides a number of static methods that you can use to retrieve information about the device's display, such as the width and height of the screen, the orientation of the device, and the device's pixel density.

Here's an example of how you might use a media query in a Flutter application :
import 'package:flutter/material.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);

    return Scaffold(
      body: Container(
        width: mediaQuery.size.width,
        height: mediaQuery.size.height,
        child: Center(
          child: Text('Hello, world!'),
        ),
      ),
    );
  }
}

In this example, we use the `MediaQuery.of()` method to retrieve a reference to the `MediaQueryData` object, which contains information about the device's display. We then use the `size` property of this object to set the width and height of a `Container` widget, which is used as the background for the page.

Advertisement