Skip to content
DeveloperMemos

Creating Flexible and Responsive UI with Row and Column in Flutter

Flutter, Dart, UI Development2 min read

When it comes to creating user interfaces in Flutter, there are many widgets available that allow for flexible and responsive designs. Two of the most commonly used widgets for this purpose are Row and Column. In this article, we'll explore how to use these widgets to create layouts that adapt to different screen sizes and orientations.

Understanding Rows and Columns

Before we dive into creating layouts, let's first understand what Row and Column widgets do. Both widgets are used to arrange children widgets horizontally or vertically, respectively.

A Row widget arranges its children in a horizontal line, while a Column widget arranges its children in a vertical column. By default, both widgets will try to fit their children within the available space on the screen. If the children exceed the available space, they can either overflow or be wrapped to the next line.

Creating a Simple Row Layout

Let's start by creating a simple row layout. In this example, we'll place three buttons side-by-side within a Row widget. Here's the code:

1Row(
2 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
3 children: [
4 ElevatedButton(
5 onPressed: () {},
6 child: Text('Button 1'),
7 ),
8 ElevatedButton(
9 onPressed: () {},
10 child: Text('Button 2'),
11 ),
12 ElevatedButton(
13 onPressed: () {},
14 child: Text('Button 3'),
15 ),
16 ],
17);

In this code, we've used the mainAxisAlignment property to specify that the three buttons should be spaced evenly within the Row. The result is that each button takes up an equal amount of space within the available width of the screen.

Creating a Simple Column Layout

Next, let's create a simple column layout. In this example, we'll place three text widgets on top of each other within a Column widget. Here's the code:

1Column(
2 crossAxisAlignment: CrossAxisAlignment.center,
3 children: [
4 Text('First item'),
5 Text('Second item'),
6 Text('Third item'),
7 ],
8);

In this code, we've used the crossAxisAlignment property to specify that the text widgets should be centered within the Column. The result is that each text widget is centered horizontally within the available width of the screen.

Creating a Responsive Layout

Now, let's create a more complex layout that adapts to different screen sizes and orientations. In this example, we'll create a Column widget that contains two Row widgets. Within the first Row, we'll place an image and some text, and within the second Row, we'll place three buttons.

Here's the code:

1Column(
2 crossAxisAlignment: CrossAxisAlignment.stretch,
3 children: [
4 Expanded(
5 child: Row(
6 children: [
7 Expanded(
8 child: Image.asset('assets/image.png'),
9 ),
10 Expanded(
11 child: Text(
12 'Lorem ipsum dolor sit amet',
13 textAlign: TextAlign.center,
14 ),
15 ),
16 ],
17 ),
18 ),
19 Expanded(
20 child: Row(
21 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
22 children: [
23 ElevatedButton(
24 onPressed: () {},
25 child: Text('Button 1'),
26 ),
27 ElevatedButton(
28 onPressed: () {},
29 child: Text('Button 2'),
30 ),
31 ElevatedButton(
32 onPressed: () {},
33 child: Text('Button 3'),
34 ),
35 ],
36 ),
37 ),
38 ],
39);

In this code, we've used the Expanded widget to ensure that the Row widgets take up as much vertical space as possible within the Column.

We've also used the crossAxisAlignment property to ensure that the image and text within the first Row are centered horizontally. Finally, we've used the mainAxisAlignment property to ensure that the buttons within the second Row are spaced evenly.

Conclusion

In this article, we've explored how to create flexible and responsive user interfaces using Row and Column widgets in Flutter. By combining these widgets with other properties such as mainAxisAlignment and crossAxisAlignment, you can create layouts that adapt to different screen sizes and orientations. With these tools at your disposal, you'll be able to create beautiful and functional user interfaces in no time!