Layouts and UI Design in Flutter
In Flutter, layouts are built using a tree of widgets. Flutter provides a rich set of layout widgets that allow you to create complex UIs with ease and flexibility.
1. Basic Layout Widgets
- Column - Arranges children vertically.
- Row - Arranges children horizontally.
- Container - A box model widget that can have margin, padding, color, and more.
- Center - Centers a child widget.
- Padding - Adds padding around a widget.
- SizedBox - Adds empty space between widgets.
Example:
Column(
children: [
Text('Welcome to Flutter'),
SizedBox(height: 10),
ElevatedButton(onPressed: () {}, child: Text('Click Me')),
],
)
Important: Column and Row widgets don’t scroll by default. To make your content scrollable, wrap them in a SingleChildScrollView.
2. Advanced Layout Widgets
- Stack - Allows widgets to overlap each other.
- Expanded and Flexible - Used inside Row or Column to take up available space.
- ListView - A scrollable list of widgets.
- GridView - Displays items in a grid format.
Example with Expanded:
Row(
children: [
Expanded(child: Container(color: Colors.red, height: 100)),
Expanded(child: Container(color: Colors.green, height: 100)),
],
)
Note: Use Flexible when you want a child to take only as much space as it needs within the flexible space.
3. Responsive Design
To make your UI responsive, use MediaQuery to get screen size or use packages like flutter_screenutil or responsive_builder.
Example using MediaQuery:
Container(
width: MediaQuery.of(context).size.width * 0.8,
height: 200,
color: Colors.blue,
)
Summary:
- Use Column and Row to arrange items vertically and horizontally.
- Wrap layout widgets with Container, Padding, or Center for better spacing and alignment.
- Use Expanded or Flexible to handle available space smartly.
- Use ListView and GridView for scrollable content.
- Make use of MediaQuery for responsive UI.
Flutter Widgets and Layouts
Layouts and UI Design in Flutter
In Flutter, layouts are built using a tree of widgets. Flutter provides a rich set of layout widgets that allow you to create complex UIs with ease and flexibility.
1. Basic Layout Widgets
- Column - Arranges children vertically.
- Row - Arranges children horizontally.
- Container - A box model widget that can have margin, padding, color, and more.
- Center - Centers a child widget.
- Padding - Adds padding around a widget.
- SizedBox - Adds empty space between widgets.
Example:
Column(
children: [
Text('Welcome to Flutter'),
SizedBox(height: 10),
ElevatedButton(onPressed: () {}, child: Text('Click Me')),
],
)
Important: Column and Row widgets don’t scroll by default. To make your content scrollable, wrap them in a SingleChildScrollView.
2. Advanced Layout Widgets
- Stack - Allows widgets to overlap each other.
- Expanded and Flexible - Used inside Row or Column to take up available space.
- ListView - A scrollable list of widgets.
- GridView - Displays items in a grid format.
Example with Expanded:
Row(
children: [
Expanded(child: Container(color: Colors.red, height: 100)),
Expanded(child: Container(color: Colors.green, height: 100)),
],
)
Note: Use Flexible when you want a child to take only as much space as it needs within the flexible space.
3. Responsive Design
To make your UI responsive, use MediaQuery to get screen size or use packages like
flutter_screenutil or
responsive_builder.
Example using MediaQuery:
Container(
width: MediaQuery.of(context).size.width * 0.8,
height: 200,
color: Colors.blue,
)
Summary:
- Use Column and Row to arrange items vertically and horizontally.
- Wrap layout widgets with Container, Padding, or Center for better spacing and alignment.
- Use Expanded or Flexible to handle available space smartly.
- Use ListView and GridView for scrollable content.
- Make use of MediaQuery for responsive UI.
For deeper understanding of layout widgets, you can explore Flutter's Layout documentation.
For deeper understanding of layout widgets, you can explore Flutter's Layout documentation.