MediaQuery in Flutter

MediaQuery gives you access to device and screen information like:

  • Screen size (width & height)
  • Orientation (portrait/landscape)
  • Padding (safe area insets)
  • Pixel ratio (for HD displays)
  • Text scaling

Source Code​

				
					@override
Widget build(BuildContext context) {
  final screenWidth = MediaQuery.of(context).size.width;

  return Scaffold(
    appBar: AppBar(title: Text('MediaQuery Example')),
    body: Center(
      child: Container(
        width: screenWidth * 0.8, // 80% of screen width
        height: 200,
        color: Colors.teal,
        child: Center(
          child: Text(
            'Responsive Container',
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
    ),
  );
}

				
			

Ai Code Analizer

📐 Most Useful MediaQuery Properties

PropertyDescription
MediaQuery.of(context).sizeReturns Size(width, height) of the screen
orientationReturns Orientation.portrait or landscape
paddingSafe area insets (top notch, bottom bar, etc.)
devicePixelRatioNumber of device pixels per logical pixel
textScaleFactorUser-defined font scaling factor
viewInsetsInfo on keyboard space (useful for input forms)