What is the difference between MediaQuery and LayoutBuilder?
MediaQuery.of(context).size gives the entire screen dimensions. LayoutBuilder constraints give the exact space the parent has allocated to this widget, which may be smaller.
Build layouts that adapt to different screen sizes and platforms
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Flutter apps can run on phones, tablets, desktops, and the web, each with very different screen sizes. Responsive design means your layout adjusts smoothly to any screen width and height. Adaptive design goes a step further by changing the actual UI pattern based on the platform or device. For example, a phone might show a bottom navigation bar while a tablet shows a side navigation rail. This lesson covers MediaQuery for reading screen dimensions, LayoutBuilder for building widgets relative to their parent, and practical patterns for handling multiple screen sizes.
MediaQuery gives you access to the current screen size, orientation, text scale factor, padding for notches and system bars, and more. You access it through MediaQuery.of(context) and it returns a MediaQueryData object with all the information about the current display environment.
1@override2Widget build(BuildContext context) {3final screenWidth = MediaQuery.of(context).size.width;4final screenHeight = MediaQuery.of(context).size.height;5final orientation = MediaQuery.of(context).orientation;6final topPadding = MediaQuery.of(context).padding.top; // notch/status bar7final textScale = MediaQuery.of(context).textScaleFactor;89// Responsive layout based on width10if (screenWidth >= 1200) {11return DesktopLayout(); // wide screens12} else if (screenWidth >= 600) {13return TabletLayout(); // medium screens14} else {15return MobileLayout(); // small screens16}17}
While MediaQuery gives you the full screen size, LayoutBuilder tells you the exact space available to a specific widget from its parent. This is more useful when building reusable components that might be placed inside different containers at different sizes.
1// LayoutBuilder adapts to the available space2LayoutBuilder(3builder: (context, constraints) {4// constraints.maxWidth = available width from parent5if (constraints.maxWidth > 600) {6// Wide: show grid with 3 columns7return GridView.builder(8gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(9crossAxisCount: 3,10crossAxisSpacing: 16,11mainAxisSpacing: 16,12),13itemCount: items.length,14itemBuilder: (_, i) => ItemCard(item: items[i]),15);16} else {17// Narrow: show simple list18return ListView.builder(19itemCount: items.length,20itemBuilder: (_, i) => ItemTile(item: items[i]),21);22}23},24)
Instead of using fixed pixel values, use Flex widgets to make children take up a proportion of the available space. Expanded takes all remaining space, while Flexible takes only as much as it needs up to the available space. You can combine multiple Expanded widgets with different flex values to create ratio-based layouts.
1// Proportional layout using flex2Row(3children: [4// Takes 2/3 of the row width5Expanded(6flex: 2,7child: ContentPanel(),8),9const SizedBox(width: 16),10// Takes 1/3 of the row width11Expanded(12flex: 1,13child: SidePanel(),14),15],16)1718// FractionallySizedBox for percentage-based sizing19FractionallySizedBox(20widthFactor: 0.8, // 80% of parent width21child: ElevatedButton(22onPressed: () {},23child: const Text('Wide Button'),24),25)2627// AspectRatio to maintain proportions28AspectRatio(29aspectRatio: 16 / 9, // width:height ratio30child: Container(color: Colors.blue),31)
A common pattern is to show different navigation styles based on screen width. Phones get a BottomNavigationBar, tablets get a NavigationRail on the side, and desktops get a full NavigationDrawer that is always visible. Here is how to build that.
1class AdaptiveScaffold extends StatefulWidget {2@override3State<AdaptiveScaffold> createState() => _AdaptiveScaffoldState();4}56class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {7int _selectedIndex = 0;89final _pages = const [10HomeScreen(),11SearchScreen(),12ProfileScreen(),13];1415@override16Widget build(BuildContext context) {17final width = MediaQuery.of(context).size.width;1819// Desktop: permanent side drawer20if (width >= 1200) {21return Scaffold(22body: Row(23children: [24NavigationDrawer(25selectedIndex: _selectedIndex,26onDestinationSelected: (i) => setState(() => _selectedIndex = i),27children: const [28NavigationDrawerDestination(icon: Icon(Icons.home), label: Text('Home')),29NavigationDrawerDestination(icon: Icon(Icons.search), label: Text('Search')),30NavigationDrawerDestination(icon: Icon(Icons.person), label: Text('Profile')),31],32),33Expanded(child: _pages[_selectedIndex]),34],35),36);37}3839// Tablet: navigation rail on the side40if (width >= 600) {41return Scaffold(42body: Row(43children: [44NavigationRail(45selectedIndex: _selectedIndex,46onDestinationSelected: (i) => setState(() => _selectedIndex = i),47labelType: NavigationRailLabelType.all,48destinations: const [49NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),50NavigationRailDestination(icon: Icon(Icons.search), label: Text('Search')),51NavigationRailDestination(icon: Icon(Icons.person), label: Text('Profile')),52],53),54const VerticalDivider(width: 1),55Expanded(child: _pages[_selectedIndex]),56],57),58);59}6061// Phone: bottom navigation bar62return Scaffold(63body: _pages[_selectedIndex],64bottomNavigationBar: BottomNavigationBar(65currentIndex: _selectedIndex,66onTap: (i) => setState(() => _selectedIndex = i),67items: const [68BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),69BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),70BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),71],72),73);74}75}
A widely used set of breakpoints is: below 600 pixels for phones, 600 to 1199 pixels for tablets, and 1200 pixels and above for desktops. These are not strict rules but a good starting point. Always test your app on multiple screen sizes.
Never use fixed pixel widths for layouts that need to fill the screen. Use Expanded, Flexible, FractionallySizedBox, or MediaQuery to build layouts that adapt to any screen size. Hardcoded widths cause overflow errors on smaller screens and wasted space on larger ones.
Responsive sizing handles dimensions. Adaptive patterns handle interaction models.
Use both:
This combination creates UIs that feel natural on every device class.
Build layouts that adapt to different screen sizes and platforms. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Connect each concept to real app features you can build.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
Good widget composition is architecture at UI level, because it decides testability and reuse.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
MediaQuery.of(context).size gives the entire screen dimensions. LayoutBuilder constraints give the exact space the parent has allocated to this widget, which may be smaller.
FractionallySizedBox uses widthFactor and heightFactor to size the child as a percentage of the available space.
NavigationRail is a vertical strip of navigation icons on the side, designed for medium-width screens like tablets where a bottom bar wastes too much vertical space.
The flex factor determines the proportion of remaining space each Expanded widget gets. flex: 2 takes twice the space of flex: 1.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue