UI DESIGN-FLUTTER Lab - Week 3
UI DESIGN-FLUTTER Lab - Week 3
Lab Program 3
3. a) Design a responsive UI that adapts to different screen sizes.
MediaQuery: Helps you get the device's screen size (width and
height), allowing you to adjust your layout based on that.
LayoutBuilder: Dynamically builds the UI based on the constraints of
the available space. You can design separate layouts for small,
medium, and large screens.
Responsive layouts:
Small (Mobile) Layout: The buildMobileLayout uses Column and
Flexible widgets for stacking and flexibility.
Medium (Tablet) Layout: The buildTabletLayout uses Row and
Expanded to adjust the size of the content evenly across the
screen.
Large (Desktop) Layout: The buildDesktopLayout uses
GridView.count to divide the layout into a grid, making it ideal for
larger screens.
1. size: Provides the width and height of the screen in logical pixels.
2. orientation: Detects the current orientation of the device (portrait or
landscape).
3. padding: Returns the padding around the edges of the screen. This is
useful for handling devices with notches or rounded corners.
4. viewInsets: Provides the space reserved for the system UI, like the on-
screen keyboard.
5. devicePixelRatio: Returns the number of device pixels for each logical
pixel. This helps you scale UI elements across different pixel densities (e.g.,
low-DPI vs. high-DPI screens).
6. textScaleFactor: Defines the scaling factor for text sizes (important for
accessibility and text scaling preferences).
7. platformBrightness: Detects whether the device is in light or dark mode.
8. alwaysUse24HourFormat: Returns a boolean indicating whether the user
prefers a 24-hour clock format.
Syntax:
LayoutBuilder(
},
builder: This function is called to build the widget tree. It takes two
parameters:
context: The BuildContext.
constraints: The BoxConstraints object, which provides the minimum and
maximum width and height available for the widget.
Example Code:
import 'package:flutter/material.dart';
void main() {
runApp(ResponsiveApp());
}
Widget buildMobileLayout() {
return Column(
children: [
Flexible(
child: Container(
color: Colors.blue,
height: 100,
width: double.infinity,
child: Center(child: Text("Mobile View", style: TextStyle(color:
Colors.white))),
),
),
Flexible(
child: Container(
color: Colors.green,
height: 100,
width: double.infinity,
child: Center(child: Text("Content 1", style: TextStyle(color:
Colors.white))),
),
),
Flexible(
child: Container(
color: Colors.orange,
height: 100,
width: double.infinity,
child: Center(child: Text("Content 2", style: TextStyle(color:
Colors.white))),
),
),
],
);
}
Widget buildTabletLayout() {
return Row(
children: [
Expanded(
child: Container(
color: Colors.blue,
height: 200,
child: Center(child: Text("Tablet View", style: TextStyle(color:
Colors.white))),
),
),
Expanded(
child: Container(
color: Colors.green,
height: 200,
child: Center(child: Text("Content 1", style: TextStyle(color:
Colors.white))),
),
),
Expanded(
child: Container(
color: Colors.orange,
height: 200,
child: Center(child: Text("Content 2", style: TextStyle(color:
Colors.white))),
),
),
],
);
}
Widget buildDesktopLayout() {
return GridView.count(
crossAxisCount: 3,
children: [
Container(
color: Colors.blue,
child: Center(child: Text("Desktop View", style: TextStyle(color:
Colors.white))),
),
Container(
color: Colors.green,
child: Center(child: Text("Content 1", style: TextStyle(color:
Colors.white))),
),
Container(
color: Colors.orange,
child: Center(child: Text("Content 2", style: TextStyle(color:
Colors.white))),
),
],
);
}
}
Output:
Mobile View :
Tablet View:
Desktop View:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
home: MediaQueryExample(),
);
@override
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Container(
child: Center(
child: Text(
'Width: ${screenSize.width.toStringAsFixed(2)},
Height: ${screenSize.height.toStringAsFixed(2)}',
textAlign: TextAlign.center,
),
),
),
),
);
Output:
Note:
The 0.8 and 0.3 multipliers in the code are used to make the container size a
percentage of the screen size. Here's why:
The specific values (0.8 and 0.3) are chosen based on design preferences and
can be adjusted to fit your design needs. For example:
You could make the container wider by increasing 0.8 to 0.9 (90% of the
screen width).
You could increase the height by changing 0.3 to 0.5 (50% of the screen
height).
void main() {
runApp(ResponsiveApp());
}
return Scaffold(
appBar: AppBar(
title: Text("Responsive Layout with Custom Breakpoints"),
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// Use custom breakpoints defined in the Breakpoints class
if (screenWidth < Breakpoints.mobile) {
// Handle mobile layout with orientation detection
if (orientation == Orientation.portrait) {
return buildMobileLayout();
} else {
return buildMobileLandscapeLayout();
}
} else if (screenWidth < Breakpoints.tablet) {
// Handle tablet layout
return buildTabletLayout();
} else {
// Handle desktop layout
return buildDesktopLayout();
}
},
),
);
}
children: [
Container(
color: Colors.blue,
width: 100,
height: double.infinity,
child: Center(child: Text("Mobile Layout (Landscape)", style:
TextStyle(color: Colors.white))),
),
Expanded(
child: Container(
color: Colors.green,
child: Center(child: Text("Content 1", style: TextStyle(color:
Colors.white))),
),
),
Expanded(
child: Container(
color: Colors.orange,
child: Center(child: Text("Content 2", style: TextStyle(color:
Colors.white))),
),
),
],
);
}
Container(
color: Colors.orange,
height: 100,
child: Center(child: Text("Content 2", style: TextStyle(color:
Colors.white))),
),
],
),
),
],
);
}
Output:
3. Tablet Layout
4. Desktop Layout