This app shows different text depending on the screen width. If the screen is less than 600 pixels wide, it shows "This is a small screen". Otherwise, it shows "This is a large screen".
Flutter
import'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Responsive Layout Example')),
body: LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < 600) {
return const Center(child: Text('This is a small screen'));
} else {
return const Center(child: Text('This is a large screen'));
}
},
),
),
);
}
}
OutputSuccess
Important Notes
Use LayoutBuilder to get the size of the parent widget and adapt layout accordingly.
MediaQuery gives the full screen size but LayoutBuilder is better for parts of the screen.
Test your app on different devices or use device simulators to see how layouts change.
Summary
Responsive layouts help your app look good on all screen sizes.
Use LayoutBuilder or MediaQuery to get screen size.
Change widgets or layout direction based on screen width.
Practice
(1/5)
1. What is the main purpose of using responsive layout patterns in Flutter apps?
easy
A. To make the app look good on different screen sizes
B. To add animations to the app
C. To improve app performance by caching data
D. To change the app's theme colors automatically
Solution
Step 1: Understand responsive layout meaning
Responsive layouts adjust UI to fit various screen sizes and orientations.
Step 2: Identify the main goal
The goal is to ensure the app looks good and works well on phones, tablets, and other devices.
Final Answer:
To make the app look good on different screen sizes -> Option A
Quick Check:
Responsive layout = good appearance on all screens [OK]
Hint: Responsive means adapting UI to screen size [OK]
Common Mistakes:
Confusing responsive layout with animations
Thinking it improves performance directly
Mixing it with theme or color changes
2. Which Flutter widget helps you get the current screen size inside its builder function?
easy
A. Container
B. MediaQuery
C. LayoutBuilder
D. Scaffold
Solution
Step 1: Recall widgets for screen size
MediaQuery provides screen info globally, but LayoutBuilder gives constraints inside its builder.
Step 2: Identify widget with builder function
LayoutBuilder has a builder that provides BoxConstraints with maxWidth and maxHeight.
Final Answer:
LayoutBuilder -> Option C
Quick Check:
LayoutBuilder = widget with builder for size [OK]
Hint: LayoutBuilder uses builder with constraints [OK]
Common Mistakes:
Choosing MediaQuery which is not a builder widget
Picking Container or Scaffold which don't provide size info
Confusing MediaQuery and LayoutBuilder roles
3. Given this Flutter code snippet, what widget will be shown on a screen width of 500 pixels?
A. Row and Column are used incorrectly for screen sizes
B. Using Row for small screens may cause overflow horizontally
C. No error; code works fine for responsive layout
D. Missing MediaQuery to get screen size
Solution
Step 1: Analyze layout for small screens
For small screens, a Row with two Text widgets may overflow horizontally.
Step 2: Understand overflow risk
Row arranges children horizontally, which can cause overflow if width is limited.
Step 3: Check other options
MediaQuery is not required here; LayoutBuilder provides constraints. Using Column on large screens is fine.
Final Answer:
Using Row for small screens may cause overflow horizontally -> Option B
Quick Check:
Row on narrow screen risks overflow [OK]
Hint: Row on narrow screen can overflow horizontally [OK]
Common Mistakes:
Thinking code has no error
Believing MediaQuery is mandatory here
Confusing Row and Column usage
5. You want to create a responsive Flutter layout that shows a vertical list on narrow screens and a grid with 3 columns on wide screens. Which approach is best?
hard
A. Use a fixed Container size and ignore screen width
B. Use MediaQuery to get screen height and switch between ListView and GridView
C. Always use GridView with 3 columns regardless of screen size
D. Use LayoutBuilder to check maxWidth and return ListView or GridView accordingly
Solution
Step 1: Decide how to detect screen width
LayoutBuilder provides maxWidth constraints inside builder, ideal for responsive layout.
Step 2: Choose widgets based on width
Return ListView for narrow screens and GridView with 3 columns for wide screens.
Step 3: Evaluate other options
MediaQuery height is not relevant; fixed Container ignores responsiveness; always GridView ignores narrow screens.
Final Answer:
Use LayoutBuilder to check maxWidth and return ListView or GridView accordingly -> Option D