Responsive UI in Flutter with MediaQuery and LayoutBuilder

Responsive UI in Flutter with MediaQuery and LayoutBuilder

Creating a beautiful app is great — but making it look perfect across all screen sizes is even better. In Flutter, two powerful tools help you build responsive UI:

  • MediaQuery

  • LayoutBuilder

In this blog, you’ll learn how to use both to make your app adaptive and responsive across devices like phones, tablets, and desktops.


πŸ” What is Responsive Design?

Responsive UI adapts your app’s layout to different screen sizes, resolutions, and orientations.

One layout for all screens? ❌
One layout that adapts? ✅


🧰 Tools Flutter Offers for Responsiveness

ToolWhat it does
MediaQueryGives screen size, orientation, etc.
LayoutBuilderBuilds widgets based on constraints
AspectRatioMaintains widget width-height ratios
FittedBoxScales child widget to fit space
Flexible / ExpandedProportional space in Row/Column

πŸ§ͺ Example 1: Using MediaQuery

MediaQuery lets you fetch screen info like width, height, orientation, etc.

✅ Use Case: Changing layout based on screen width

import 'package:flutter/material.dart'; class MediaQueryExample extends StatelessWidget { @override Widget build(BuildContext context) { final screenWidth = MediaQuery.of(context).size.width; return Scaffold( appBar: AppBar(title: Text("MediaQuery Example")), body: screenWidth < 600 ? _buildMobileLayout() : _buildTabletLayout(), ); } Widget _buildMobileLayout() { return Center(child: Text("Mobile Layout")); } Widget _buildTabletLayout() { return Center(child: Text("Tablet/Desktop Layout")); } }

πŸ“ What MediaQuery Provides

MediaQuery.of(context).size.width // screen width MediaQuery.of(context).size.height // screen height MediaQuery.of(context).orientation // portrait or landscape MediaQuery.of(context).padding // safe area padding MediaQuery.of(context).devicePixelRatio // pixel density

πŸ§ͺ Example 2: Using LayoutBuilder

LayoutBuilder gives you constraints of the widget — helpful for local responsiveness.

✅ Use Case: Change widget size based on available space

import 'package:flutter/material.dart'; class LayoutBuilderExample extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("LayoutBuilder Example")), body: LayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth < 600) { return _buildMobileLayout(); } else { return _buildTabletLayout(); } }, ), ); } Widget _buildMobileLayout() { return Center(child: Text("Mobile View")); } Widget _buildTabletLayout() { return Center(child: Text("Tablet View")); } }

πŸ” When to Use What?

Use CaseUse
Need entire screen infoMediaQuery
React to local container constraintsLayoutBuilder
Combine global + local layout infoUse both together

🧩 Example 3: Responsive Grid with LayoutBuilder

GridView.count( crossAxisCount: constraints.maxWidth < 600 ? 2 : 4, children: List.generate(20, (index) { return Card( child: Center(child: Text('Item $index')), ); }), );

🎨 Responsive Widgets

  • Flexible & Expanded: distribute space in rows/columns

  • FittedBox: scale content inside containers

  • AspectRatio: maintain consistent width/height

  • Wrap: create multi-line rows that wrap automatically

  • FractionallySizedBox: size child as a fraction of parent


πŸ“± Adaptive Layout Example with Combined Approach

class ResponsiveExample extends StatelessWidget { @override Widget build(BuildContext context) { final screenWidth = MediaQuery.of(context).size.width; return Scaffold( appBar: AppBar(title: Text("Responsive Layout")), body: LayoutBuilder( builder: (context, constraints) { if (screenWidth < 480) { return _buildSmallScreen(); } else if (screenWidth < 800) { return _buildMediumScreen(); } else { return _buildLargeScreen(); } }, ), ); } Widget _buildSmallScreen() => Center(child: Text("Mobile")); Widget _buildMediumScreen() => Center(child: Text("Tablet")); Widget _buildLargeScreen() => Center(child: Text("Desktop")); }

πŸ§ͺ Best Practices for Responsive Flutter Apps

✅ Define breakpoints (mobile < 600, tablet < 1024, etc.)
✅ Use Flexible for resizing within Row/Column
✅ Use SafeArea for notch/padding handling
✅ Avoid hardcoded widths/heights — use percentages or constraints
✅ Test on multiple screen sizes/emulators


πŸ“± Recommended Breakpoints (Guideline)

DeviceWidth (px)
Small Phone< 480
Phone480–599
Tablet600–1023
Desktop1024+

πŸ’‘ Packages to Help

PackageUse
flutter_screenutilResponsive text and dimensions
responsive_builderEasily build layouts per screen size
device_previewPreview your app on multiple devices
flutter_responsiveWidget-based responsiveness helper

πŸ”„ Testing Responsiveness

✅ Use Device Preview plugin in Flutter
✅ Manually resize Chrome with Flutter Web
✅ Run app in various emulators and screen sizes
✅ Try in both portrait and landscape orientations


πŸš€ Final Thoughts

Flutter gives you full control over responsive layout, whether you’re building for mobile, tablet, desktop, or web.

Use:

  • MediaQuery for global screen info

  • LayoutBuilder for contextual layout changes

  • Responsive design patterns to keep UI consistent and adaptive




Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview