Layouts & Responsive Design
Flutter provides powerful layout widgets that let you create responsive designs that work across different screen sizes. Understanding these layouts is key to building beautiful apps.
Common Layout Widgets
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Layout Widgets โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ Row โ Horizontal arrangement โ
โ โโโโโฌโโโโฌโโโโ โ
โ โ A โ B โ C โ โ
โ โโโโโดโโโโดโโโโ โ
โ โ
โ Column โ Vertical arrangement โ
โ โโโโโ โ
โ โ A โ โ
โ โโโโโค โ
โ โ B โ โ
โ โโโโโค โ
โ โ C โ โ
โ โโโโโ โ
โ โ
โ Stack โ Overlapping elements โ
โ โโโโโโโโโโโโโโ โ
โ โ Background โ โ
โ โ โโโโโ โ โ
โ โ โ B โ โ Overlay โ
โ โ โโโโโ โ โ
โ โโโโโโโโโโโโโโ โ
โ โ
โ Wrap โ Flow with wrapping โ
โ โโโโโฌโโโโฌโโโโ โ
โ โ A โ B โ C โ โ
โ โโโโโผโโโโผโโโโค โ
โ โ D โ E โ F โ โ
โ โโโโโดโโโโดโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Responsive Layout Example
import 'package:flutter/material.dart';
class ResponsiveLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 600) {
return _buildWideLayout();
} else {
return _buildNarrowLayout();
}
},
),
);
}
Widget _buildWideLayout() {
return Row(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.blue, child: Center(child: Text('Side Menu'))),
),
Expanded(
flex: 3,
child: Container(color: Colors.grey[200], child: Center(child: Text('Main Content'))),
),
],
);
}
Widget _buildNarrowLayout() {
return Column(
children: [
Expanded(child: Container(color: Colors.blue, child: Center(child: Text('Header')))),
Expanded(flex: 3, child: Container(color: Colors.grey[200], child: Center(child: Text('Content')))),
],
);
}
}
Key Tips for Responsive Design
- Use LayoutBuilder: Get parent constraints to make decisions
- Use MediaQuery: Get device screen size and orientation
- Avoid hardcoded sizes: Use percentages and flexible widgets
- Test on multiple devices: Phones, tablets, and desktop