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:
MediaQueryLayoutBuilder
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
| Tool | What it does |
|---|---|
MediaQuery | Gives screen size, orientation, etc. |
LayoutBuilder | Builds widgets based on constraints |
AspectRatio | Maintains widget width-height ratios |
FittedBox | Scales child widget to fit space |
Flexible / Expanded | Proportional 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
π What MediaQuery Provides
π§ͺ Example 2: Using LayoutBuilder
LayoutBuilder gives you constraints of the widget — helpful for local responsiveness.
✅ Use Case: Change widget size based on available space
π When to Use What?
| Use Case | Use |
|---|---|
| Need entire screen info | MediaQuery |
| React to local container constraints | LayoutBuilder |
| Combine global + local layout info | Use both together |
π§© Example 3: Responsive Grid with LayoutBuilder
π¨ Responsive Widgets
Flexible&Expanded: distribute space in rows/columnsFittedBox: scale content inside containersAspectRatio: maintain consistent width/heightWrap: create multi-line rows that wrap automaticallyFractionallySizedBox: size child as a fraction of parent
π± Adaptive Layout Example with Combined Approach
π§ͺ 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)
| Device | Width (px) |
|---|---|
| Small Phone | < 480 |
| Phone | 480–599 |
| Tablet | 600–1023 |
| Desktop | 1024+ |
π‘ Packages to Help
| Package | Use |
|---|---|
flutter_screenutil | Responsive text and dimensions |
responsive_builder | Easily build layouts per screen size |
device_preview | Preview your app on multiple devices |
flutter_responsive | Widget-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:
MediaQueryfor global screen infoLayoutBuilderfor contextual layout changesResponsive design patternsto keep UI consistent and adaptive
Comments
Post a Comment