When diving into app development with Flutter, one of the first things you'll need to grasp is how to arrange your widgets on the screen. Luckily, Flutter provides two foundational layout widgets that make this task straightforward: Rows and Columns. Together, they enable you to create intricate layouts by placing your widgets either horizontally (with Rows) or vertically (with Columns).
A Row widget allows you to arrange its child widgets in a horizontal direction. It essentially takes a series of widgets and places them side by side.
Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Icon(Icons.home), Icon(Icons.search), Icon(Icons.settings), ], )
In this example, we have a Row with three icons. The mainAxisAlignment
property is used to space the icons evenly across the available width.
Conversely, a Column widget arranges its children in a vertical direction. By utilizing Columns, you can stack widgets on top of each other.
Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text('Welcome to Flutter!'), SizedBox(height: 20), // Adds space between text and button ElevatedButton( onPressed: () {}, child: Text('Get Started'), ), ], )
Here, the Column
widget stacks a text label on top of a button, centering them vertically in the allocated space.
One of the powerful features of Flutter's layout system is that you can nest Rows and Columns to create more complex designs.
Let's take this a step further by creating a simple user interface for a profile page.
import 'package:flutter/material.dart'; class ProfilePage extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Profile'), ), body: Column( children: <Widget>[ // This is a Row that contains the profile picture and user details. Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ CircleAvatar( radius: 40, backgroundImage: NetworkImage('https://example.com/profile.jpg'), ), SizedBox(width: 20), Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( 'John Doe', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), ), Text('Flutter Developer'), ], ), ], ), Divider(height: 40), // Adds a divider between sections. // This is a Row for the buttons. Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ ElevatedButton( onPressed: () {}, child: Text('Edit Profile'), ), ElevatedButton( onPressed: () {}, child: Text('Logout'), ), ], ), ], ), ); } }
In this example, we have a profile page layout that combines Rows and Columns beautifully. The top section displays a user’s picture and name side by side using a Row. Below that, we have a Row of buttons evenly spaced across the screen. Notice how we're leveraging the properties standard to Row and Column to achieve a clean UI.
mainAxisAlignment
: Defines how the children should be placed along the main axis (horizontal for Row, vertical for Column).crossAxisAlignment
: Defines how the children should be placed along the cross axis (vertical for Row, horizontal for Column).Expanded
: Used within Rows and Columns to make a widget fill the available space.Rows and Columns are essential building blocks for creating layouts in Flutter. With just a few lines of code, you can create dynamic and responsive designs that are easy to understand and maintain.
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter
21/09/2024 | Flutter