Flutter Row and Column Widgets

Flutter Row and Column widgets serve as the building blocks for arranging elements horizontally and vertically within your application.

Understanding the Basics of Row and Column

Row and Column are flex widgets in Flutter that organize their children in a linear arrangement. The Row widget positions elements horizontally, while the Column widget arranges them vertically. Both widgets inherit from the Flex class and share many properties, making them consistent in their usage patterns.

Key Properties of Row and Column

When working with Row and Column, several properties control how child widgets are positioned:

  • mainAxisAlignment: Controls how children are positioned along the main axis (horizontal for Row, vertical for Column)
  • crossAxisAlignment: Determines how children are aligned along the cross axis (vertical for Row, horizontal for Column)
  • mainAxisSize: Specifies how much space the Row or Column should occupy along the main axis
  • children: The list of widgets to display in the Row or Column

MainAxisAlignment: Controlling Primary Positioning

The mainAxisAlignment property offers six different options to position child widgets:

  1. MainAxisAlignment.start: Places children at the start of the main axis
  2. MainAxisAlignment.end: Positions children at the end of the main axis
  3. MainAxisAlignment.center: Centers children along the main axis
  4. MainAxisAlignment.spaceBetween: Distributes space evenly between children, with none at the edges
  5. MainAxisAlignment.spaceAround: Distributes space evenly between children, with half as much space at the edges
  6. MainAxisAlignment.spaceEvenly: Distributes space evenly between children and at the edges
Row(  
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,  
  children: [  
    Container(color: Colors.red, height: 50, width: 50),  
    Container(color: Colors.blue, height: 50, width: 50),  
    Container(color: Colors.green, height: 50, width: 50),  
  ],  
)  

CrossAxisAlignment: Perfecting Secondary Alignment

The crossAxisAlignment property offers five options for positioning children along the cross axis:

  1. CrossAxisAlignment.start: Aligns children to the start of the cross axis
  2. CrossAxisAlignment.end: Aligns children to the end of the cross axis
  3. CrossAxisAlignment.center: Centers children along the cross axis
  4. CrossAxisAlignment.stretch: Stretches children to fill the cross axis
  5. CrossAxisAlignment.baseline: Aligns children by their baselines (text alignment)
Column(  
  crossAxisAlignment: CrossAxisAlignment.start,  
  children: [  
    Text(\"Flutter\"),  
    Text(\"Row\"),  
    Text(\"Column\"),  
  ],  
)  

MainAxisSize: Controlling Space Consumption

The mainAxisSize property determines how much space the Row or Column should occupy along its main axis:

  • MainAxisSize.max: The default, fills all available space along the main axis
  • MainAxisSize.min: Takes up only as much space as needed by its children
Row(  
  mainAxisSize: MainAxisSize.min,  
  children: [  
    Icon(Icons.star),  
    Text(\"Rated 4.9\"),  
  ],  
)  

Creating Complex Layouts with Nested Row and Column

To create sophisticated UI layouts, you can nest Row and Column widgets within each other. This nesting allows for the creation of grid-like structures and complex arrangements.

Column(  
  children: [  
    Row(  
      mainAxisAlignment: MainAxisAlignment.spaceBetween,  
      children: [  
        Text(\"User Profile\"),  
        Icon(Icons.settings),  
      ],  
    ),  
    SizedBox(height: 20),  
    Row(  
      mainAxisAlignment: MainAxisAlignment.center,  
      children: [  
        CircleAvatar(radius: 50),  
      ],  
    ),  
    SizedBox(height: 20),  
    Row(  
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,  
      children: [  
        Column(children: [Icon(Icons.phone), Text(\"Call\")]),  
        Column(children: [Icon(Icons.message), Text(\"Message\")]),  
        Column(children: [Icon(Icons.video_call), Text(\"Video\")]),  
      ],  
    ),  
  ],  
)  

Advanced Techniques and Best Practices

Using Expanded and Flexible Widgets

The Expanded and Flexible widgets work with Row and Column to create responsive layouts:

  • Expanded: Forces a child to fill available space along the main axis
  • Flexible: Similar to Expanded but with control over the flex factor
Row(  
  children: [  
    Container(color: Colors.blue, width: 100),  
    Expanded(  
      child: Container(color: Colors.red),  
    ),  
    Container(color: Colors.yellow, width: 100),  
  ],  
)  

Using multiple Expanded widgets distributes space proportionally:

Row(  
  children: [  
    Expanded(  
      flex: 1,  
      child: Container(color: Colors.red),  
    ),  
    Expanded(  
      flex: 2,  
      child: Container(color: Colors.green),  
    ),  
  ],  
)  

Working with Overflow

When children don't fit within a Row or Column, overflow occurs. Handle this with:

  • Wrapping the Row or Column in a SingleChildScrollView
  • Using Wrap widget instead of Row or Column
  • Applying Flexible or Expanded to children
SingleChildScrollView(  
  scrollDirection: Axis.horizontal,  
  child: Row(  
    children: List.generate(  
      10,  
      (index) => Container(  
        width: 150,  
        height: 100,  
        color: Colors.primaries[index % Colors.primaries.length],  
        margin: EdgeInsets.all(8),  
        child: Center(child: Text('Item $index')),  
      ),  
    ),  
  ),  
)  

Practical Examples

Creating a Profile Card

Card(  
  child: Padding(  
    padding: EdgeInsets.all(16.0),  
    child: Column(  
      crossAxisAlignment: CrossAxisAlignment.start,  
      mainAxisSize: MainAxisSize.min,  
      children: [  
        Row(  
          children: [  
            CircleAvatar(radius: 30),  
            SizedBox(width: 16),  
            Column(  
              crossAxisAlignment: CrossAxisAlignment.start,  
              children: [  
                Text('John Doe', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),  
                Text('Flutter Developer'),  
              ],  
            ),  
          ],  
        ),  
        SizedBox(height: 16),  
        Text('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'),  
        SizedBox(height: 16),  
        Row(  
          mainAxisAlignment: MainAxisAlignment.spaceBetween,  
          children: [  
            TextButton(onPressed: () {}, child: Text('MESSAGE')),  
            TextButton(onPressed: () {}, child: Text('FOLLOW')),  
          ],  
        ),  
      ],  
    ),  
  ),  
)  

Building a Custom App Bar

Container(  
  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),  
  color: Colors.blue,  
  child: Row(  
    children: [  
      IconButton(icon: Icon(Icons.menu, color: Colors.white), onPressed: () {}),  
      Expanded(  
        child: Text(  
          'Flutter Row & Column',  
          style: TextStyle(color: Colors.white, fontSize: 20),  
          textAlign: TextAlign.center,  
        ),  
      ),  
      IconButton(icon: Icon(Icons.search, color: Colors.white), onPressed: () {}),  
    ],  
  ),  
)  

Related Topics