How do you get screen width in Flutter?
MediaQuery provides screen and device information.
Build UIs that adapt to any screen size
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
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
Responsive design means building interfaces that look great on any screen size, from small phones to large desktop monitors. Flutter provides several tools to help you adapt your layouts.
MediaQuery gives you access to device and screen information such as width, height, safe area padding, and orientation.
1@override2Widget build(BuildContext context) {3final size = MediaQuery.of(context).size;4final width = size.width;5final height = size.height;6final padding = MediaQuery.of(context).padding; // Safe area7final orientation = MediaQuery.of(context).orientation;89return Container(10width: width * 0.9, // 90% of screen width11padding: EdgeInsets.only(top: padding.top), // Avoid notch12);13}
LayoutBuilder provides the actual constraints given to your widget, which you can use to decide which layout to render based on available space.
1LayoutBuilder(2builder: (context, constraints) {3if (constraints.maxWidth > 900) {4// Desktop layout - 3 columns5return DesktopLayout();6} else if (constraints.maxWidth > 600) {7// Tablet layout - 2 columns8return TabletLayout();9} else {10// Mobile layout - 1 column11return MobileLayout();12}13},14)
A breakpoint pattern is a reusable approach where you define screen width thresholds and switch between different layouts based on those thresholds.
1// Define breakpoints2class Breakpoints {3static const mobile = 600;4static const tablet = 900;5static const desktop = 1200;6}78// Responsive helper9class Responsive extends StatelessWidget {10final Widget mobile;11final Widget? tablet;12final Widget? desktop;1314const Responsive({15required this.mobile,16this.tablet,17this.desktop,18});1920@override21Widget build(BuildContext context) {22return LayoutBuilder(23builder: (context, constraints) {24if (constraints.maxWidth >= Breakpoints.desktop) {25return desktop ?? tablet ?? mobile;26} else if (constraints.maxWidth >= Breakpoints.tablet) {27return tablet ?? mobile;28}29return mobile;30},31);32}33}3435// Usage36Responsive(37mobile: MobileProductList(),38tablet: TabletProductGrid(),39desktop: DesktopProductTable(),40)
Always build the mobile layout first, then adapt for larger screens. Mobile is the most constrained environment, so starting there ensures your core experience works well.
Responsive UI is not only technical adaptation. It is product clarity across contexts.
Different screens should feel native to available space while keeping behavior consistent.
Build UIs that adapt to any screen size. This lesson adds one more building block for creating stable and user-friendly Flutter apps.
Build confidence with basics first; speed comes later.
After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.
Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.
MediaQuery provides screen and device information.
padding.top is the system UI padding (status bar, notch).
LayoutBuilder provides the constraints given to your widget, useful for responsive breakpoints.
Build mobile layout first, then adapt for larger screens. Mobile is the most constrained.
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