Explore practical examples of using MediaQuery, LayoutBuilder, and OrientationBuilder in Flutter to create responsive and adaptive UIs. Learn through real-world scenarios and code walkthroughs.
In this section, we delve into practical applications of MediaQuery
, LayoutBuilder
, and OrientationBuilder
to create responsive and adaptive user interfaces in Flutter. These tools are essential for building apps that provide a seamless experience across different devices and orientations. We’ll explore real-world scenarios, complete with code examples and diagrams, to illustrate how these widgets can be used effectively.
Responsive design is crucial in today’s mobile-first world, where users expect applications to work flawlessly on a variety of devices, from small smartphones to large tablets and desktops. Let’s explore how MediaQuery
, LayoutBuilder
, and OrientationBuilder
can be combined to tackle common design challenges:
We’ll walk through two example projects: a responsive dashboard layout and an adaptive profile screen. These examples will demonstrate how to use Flutter’s responsive design tools to create flexible and user-friendly interfaces.
A dashboard is a common feature in many applications, providing users with an overview of key information and controls. Let’s build a responsive dashboard that adapts to different screen sizes and orientations.
Widget build(BuildContext context) {
var mediaQuery = MediaQuery.of(context);
var screenWidth = mediaQuery.size.width;
return Scaffold(
appBar: AppBar(title: Text('Responsive Dashboard')),
body: LayoutBuilder(
builder: (context, constraints) {
return OrientationBuilder(
builder: (context, orientation) {
if (screenWidth > 1000) {
return Row(
children: [
Expanded(child: Sidebar()),
Expanded(flex: 3, child: DashboardContent()),
],
);
} else if (orientation == Orientation.landscape) {
return Column(
children: [
DashboardContent(),
Expanded(child: Sidebar()),
],
);
} else {
return DashboardContent();
}
},
);
},
),
);
}
class Sidebar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blueGrey,
child: ListView(
children: [
ListTile(title: Text('Home')),
ListTile(title: Text('Profile')),
ListTile(title: Text('Settings')),
// More items
],
),
);
}
}
class DashboardContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
Text('Dashboard Overview', style: TextStyle(fontSize: 24)),
// Additional dashboard widgets
],
),
);
}
}
Explanation:
Profile screens are another common feature in many apps. They often need to display a user’s picture and details in a way that is both attractive and functional across different devices.
Widget build(BuildContext context) {
var mediaQuery = MediaQuery.of(context);
var screenWidth = mediaQuery.size.width;
return Scaffold(
appBar: AppBar(title: Text('Profile')),
body: LayoutBuilder(
builder: (context, constraints) {
return OrientationBuilder(
builder: (context, orientation) {
if (screenWidth > 800) {
return Row(
children: [
ProfilePicture(),
Expanded(child: ProfileDetails()),
],
);
} else {
return Column(
children: [
ProfilePicture(),
ProfileDetails(),
],
);
}
},
);
},
),
);
}
class ProfilePicture extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16.0),
child: CircleAvatar(
radius: 50,
backgroundImage: NetworkImage('https://example.com/profile.jpg'),
),
);
}
}
class ProfileDetails extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Username', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
Text('Email: user@example.com'),
Text('Bio: Flutter Developer'),
// More details
],
),
);
}
}
Explanation:
ProfilePicture
and ProfileDetails
widgets are modular and reusable, promoting clean code and easy maintenance.To better understand the flow and structure of these layouts, let’s visualize the responsive profile screen layout using a Mermaid.js diagram.
graph TD A[Profile Screen] --> B[LayoutBuilder] B --> C[OrientationBuilder] C -->|Wide| D[Row Layout] C -->|Narrow| E[Column Layout] D --> F[ProfilePicture] D --> G[ProfileDetails] E --> F E --> G
Diagram Explanation:
When building responsive and adaptive UIs in Flutter, consider the following best practices:
By leveraging MediaQuery
, LayoutBuilder
, and OrientationBuilder
, you can create responsive and adaptive UIs that provide a seamless user experience across a wide range of devices. These tools allow you to build flexible layouts that adjust to different screen sizes and orientations, ensuring that your app remains functional and attractive on any device.
For further exploration, consider experimenting with these widgets in your own projects. Try creating different layouts and see how they adapt to various screen sizes and orientations. The more you practice, the more proficient you’ll become at building responsive and adaptive UIs in Flutter.