Flutter, an open-source UI toolkit from Google, is making waves in the mobile app development space. Known for its rich set of widgets, it is a go-to tool for developers seeking to create beautiful, natively-compiled apps for mobile, web, and desktop from a single codebase. In this post, we'll shed light on the practicalities of managing layout patterns in Flutter, focusing our discussion on Flutter Row and Column widgets.
Widgets are at the heart of Flutter apps; they describe what the app's view should look like with its current configuration and state. When you are creating layouts, two class instances, the row widget and column widget, become particularly relevant. Row and column widgets in Flutter help in organizing the UI into layout patterns as required.
At the core of Flutter's layout are widgets. They are the fundamental building blocks of a Flutter app's user interface. Almost everything in Flutter is observed as a widget. This includes attributes like structural items (like buttons or menu), stylistic elements (like fonts or colours), layout aspects (like padding), and many more.
Within the sphere of layout aspects, we often consider two types of widgets: Flutter Row and Column. In nutshell, the Row widget allows you to align child widgets along a horizontal line, while the column widget aligns child widgets in a vertical direction. It's like placing items horizontally or vertically on your screen as per the design requirement of your app.
Let's get our hands dirty and explore more about both row and column.
Row in Flutter is an important layout widget as it helps align child widgets in a horizontal manner. For Flutter developers, a strong grasp of Rows and their properties can prove beneficial in creating versatile layout patterns.
Creating a row in Flutter is straightforward. You simply instantiate the Row widget and provide an array of 'children' to be laid out horizontally. Here's what creating a row could look like in Flutter:
1 void main() { 2 runApp( 3 MyApp() 4 ); 5 } 6 7 class MyApp extends StatelessWidget { 8 @override 9 10 Widget build(BuildContext context) { 11 return MaterialApp( 12 home: Scaffold( 13 appBar: AppBar( 14 title: Text('Flutter Row Widget') 15 ), 16 body: Row( 17 children:[ 18 Expanded( 19 child: Text(' Column 1 ')), 20 Expanded( 21 child: Text(' Column 2 ')), 22 Expanded( 23 child: Text(' Column 3 '))]) 24 )); 25 } 26 } 27
In the example above, we've created a row with three column widgets as children of the row. The Expanded widgets around the child Text widgets imply that they should share the available horizontal space evenly among themselves.
One advantage of using the Row widget in your Flutter app is the ability to control how the child widgets are laid out. For a Flutter Row, the main axis runs horizontally, and the cross axis runs vertically. By default, a row aligns its children along the start of the row (i.e., on the left edge of the UI).
You can modify this behavior using the mainAxisAlignment and crossAxisAlignment properties. The mainAxisAlignment property determines how the row should distribute free space along the horizontal axis, and crossAxisAlignment manages alignment along the vertical axis.
Not every screen has enough real estate to accommodate all elements of a Row. Sometimes, child widgets might exceed the available space, causing an overflow. Thankfully, Flutter provides widgets that can add scrollability to Rows and other widgets.
If the children of a Row widget take up more space horizontally than the screen provides, Flutter displays an 'Overflow' message. To solve this, developers use scrolling, which allows users to view overflowing content by scrolling horizontally or vertically.
A straightforward way to make your Flutter Row scrollable involves wrapping it in a UIScrollView widget. This widget provides scrollability along one direction and can pair with a Row to provide horizontal scrolling or a Column for vertical scrolling.
Here's an example of how to make a Row scrollable:
1 Widget build(BuildContext context) { 2 return Scaffold( 3 appBar: AppBar( 4 title: Text('Flutter Scrollable Row Example') 5 ), 6 body: SingleChildScrollView( 7 scrollDirection: Axis.horizontal, 8 child: Row( 9 children:<Widget>[ 10 for (int i = 0; i < 50; i++) 11 Container( 12 width: 80, 13 child:Text('Container $i', 14 style:TextStyle(fontSize: 30) 15 ) 16 ) 17 ] 18 ) 19 ) 20 ); 21 } 22
In the above example, a SingleChildScrollView is used to wrap the Row, thus making it scrollable. The scrollDirection property is set to Axis.horizontal, instructing the SingleChildScrollView to provide a scrollable list in a horizontal direction.
Scrollability is not limited to Rows. With the same approach, a Flutter scrollable Column can also be achieved.
Much like the Row widget, the Column widget is yet another essential tool in the Flutter developer's toolbox. A column organizes its children in a vertical line, positioning them one after the other from top to bottom.
Creating a column in Flutter is easy. We use the Column widget and pass an array of 'children' to be laid out vertically. The following is a straightforward Column creation example in Flutter:
1 Widget build(BuildContext context) { 2 return MaterialApp( 3 home: Scaffold( 4 appBar: AppBar( 5 title: Text('Flutter Column Widget Example'), 6 ), 7 body: Column( 8 children: <Widget>[ 9 Text('First child', style: TextStyle(fontSize: 25)), 10 Text('Second child', style: TextStyle(fontSize: 25)), 11 Text('Third child', style: TextStyle(fontSize: 25)), 12 ], 13 ), 14 ), 15 ); 16 } 17
In the example above, we have created a column with three Text widgets as children of the column. These Text widgets would be displayed one below the other, stacked vertically.
When dealing with the Column widget, the main axis runs vertically, and the cross axis runs horizontally. The mainAxisAlignment and crossAxisAlignment properties work the same way as in Rows, only in a flipped manner. By default, a Column aligns its children along the top of the widget (i.e., at the screen's top edge).
Similar to Rows, there might be instances when the children of a Column widget can take up more vertical space than the screen can provide, causing an 'Overflow' message. This is where the SingleChildScrollView comes into play again, making a widget like the Column scrollable.
Analogous to the concept with rows, if the collective height of all the children widgets within a column exceeds the column's height, an overflow error occurs. To handle cases like these, we need the columns to be scrollable as well.
To implement a scrollable Column with SingleChildScrollView, we wrap our Column widget with the SingleChildScrollView widget. Below is a simple example of a Flutter scrollable Column:
1 Widget build(BuildContext context) { 2 return MaterialApp( 3 home: Scaffold( 4 appBar: AppBar( 5 title: Text('Flutter Scrollable Column Example') 6 ), 7 body: SingleChildScrollView( 8 child: Column( 9 children: <Widget>[ 10 for (int i = 0; i< 50; i++) 11 Container( 12 height: 80, 13 child: Text('Container $i', 14 style: TextStyle(fontSize: 30)), 15 ), 16 ] 17 ) 18 ), 19 ), 20 ); 21 } 22
In this example, the Column is a child of SingleChildScrollView, and by default, the ScrollDirection is set to vertical.
A single row or column is bearable for building simple layouts. Still, for more complex design requirements, you might have to venture out to using a combination of both. Flutter column and row can be powerful when used together.
You might use a combination of row and column widgets when your UI design demands a more complex arrangement of widgets. For example, a single column might have several child widgets distributed along the vertical axis, and one of these child widgets could further be a row having its own children, arranged in a horizontal manner.
By nesting rows and columns, you can give a highly customizable structure to the user interface. Here is a basic example:
1 Widget build(BuildContext context) { 2 return MaterialApp( 3 home: Scaffold( 4 appBar: AppBar( 5 title: Text('Flutter Row Column Mix Layout Example'),), 6 body: Column( 7 children: <Widget>[ 8 Text('Start of Column', 9 style: TextStyle(fontSize: 30)), 10 Row( 11 children: <Widget>[ 12 Text('Start of Row', 13 style: TextStyle(fontSize: 30)), 14 Text('End of Row', 15 style: TextStyle(fontSize: 30)), 16 ], 17 ), 18 Text('End of Column', 19 style: TextStyle(fontSize: 30)), 20 ], 21 ), 22 ), 23 ); 24 } 25
In this Flutter Column and Row example, you can see that we used Column as the parent widget. Inside the Column, we have other widgets, including another Row widget that contains more child widgets.
Even though this example is relatively straightforward, combining Flutter row and column widgets can build complex layouts suitable for various types of mobile applications.
As beneficial as Rows and Columns in Flutter are, they're not without their own set of challenges. Many times, while developing a Flutter app, developers face issues that are seemingly puzzling. Hence, it's crucial to have a troubleshooter guide.
The chances are high that, as a Flutter developer, you've run into the 'Yellow and Black Striped' warning once or twice while using Rows or Columns. This caution points to the infamous 'Overflow' error, which means that the children of your Row or Column widget are demanding more space than they have access to.
The primary solution is to either reduce the size of the child widgets or make the parent Row or Column scrollable using SingleChildScrollView as we have discussed above.
Another common problem involves aligning the child widgets as per the design requirement. This can usually be resolved by understanding and correctly using the mainAxisAlignment and crossAxisAlignment properties of the Row or Column widget.
Here are some best practices to keep in mind for a smoother use of Flutter row and column:
Understanding and mastering the use of Row and Column widgets in Flutter opens up a universe of layout patterns for developers. These widgets offer significant control over how the app's UI shapes up, providing options to align, distribute, and manage space between child widgets.
Here, we commenced by introducing what Row and Column widgets in Flutter signify. We then dug deeper and learned how to create basic Row and Column layouts, along with understanding their main and cross axis alignments. We discovered how to create a Flutter scrollable Row and Column and used the SingleChildScrollView widget. Finally, we probed into the combination of both the Flutter Row and Column widgets, created a mixed layout, and even looked at some common problems and their solutions.
To learn more about the Row and Column widgets, and see other examples, visit the official Flutter docs. For a more hands-on approach, there are numerous tutorials and articles available from seasoned Flutter developers that can help to advance your understanding and skills further.
Thank you for reading this blog post, and happy Fluttering!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.