FlutterResponsive DesignUI/UX
Building Responsive Flutter Apps
•2 min read

title: Building Responsive Flutter Apps excerpt: Learn how to create Flutter applications that look great on any screen size, from phones to tablets. publishedDate: "2024-10-20" tags: ["Flutter", "Responsive Design", "UI/UX"] coverImage: "/images/blog/flutter-responsive.jpg" isDraft: false
Building Responsive Flutter Apps
Creating apps that adapt to different screen sizes is essential for a great user experience. Let's explore how to build truly responsive Flutter applications.
Understanding Screen Sizes
Flutter provides several ways to get screen information:
// Get screen size
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
// Get device pixel ratio
final pixelRatio = MediaQuery.of(context).devicePixelRatio;
// Check orientation
final isLandscape = MediaQuery.of(context).orientation == Orientation.landscape;LayoutBuilder Widget
LayoutBuilder gives you the constraints of the parent widget:
class ResponsiveLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < 600) {
return MobileLayout();
} else if (constraints.maxWidth < 1200) {
return TabletLayout();
} else {
return DesktopLayout();
}
},
);
}
}Creating a Responsive Helper
Here's a utility class for responsive design:
class Responsive {
static bool isMobile(BuildContext context) =>
MediaQuery.of(context).size.width < 600;
static bool isTablet(BuildContext context) =>
MediaQuery.of(context).size.width >= 600 &&
MediaQuery.of(context).size.width < 1200;
static bool isDesktop(BuildContext context) =>
MediaQuery.of(context).size.width >= 1200;
static T value<T>(
BuildContext context, {
required T mobile,
T? tablet,
T? desktop,
}) {
if (isDesktop(context)) return desktop ?? tablet ?? mobile;
if (isTablet(context)) return tablet ?? mobile;
return mobile;
}
}Usage:
Padding(
padding: EdgeInsets.all(
Responsive.value(
context,
mobile: 16,
tablet: 24,
desktop: 32,
),
),
child: YourWidget(),
)Flexible and Expanded
Use Flexible and Expanded for proportional layouts:
Row(
children: [
Expanded(
flex: 2,
child: Container(color: Colors.red),
),
Expanded(
flex: 1,
child: Container(color: Colors.blue),
),
],
)Responsive Grid
Create adaptive grids with GridView:
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: Responsive.value(
context,
mobile: 2,
tablet: 3,
desktop: 4,
),
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: items.length,
itemBuilder: (context, index) => ItemCard(item: items[index]),
)Best Practices
Always test your app on multiple screen sizes during development.
- Use relative sizing - Avoid hardcoded pixel values
- Test on real devices - Emulators don't always reflect reality
- Consider orientation - Support both portrait and landscape
- Use breakpoints consistently - Define them once, use everywhere
Conclusion
Building responsive Flutter apps requires thinking about layout from the start. Use the tools Flutter provides, and your apps will look great on any device!