logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Building Layouts with Flutter

author
Generated by
Nitish Kumar Singh

21/09/2024

Flutter

Sign in to read full article

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).

Understanding Rows and Columns

Rows

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.

Example of a Simple Row:

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.

Columns

Conversely, a Column widget arranges its children in a vertical direction. By utilizing Columns, you can stack widgets on top of each other.

Example of a Simple Column:

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.

Combining Rows and Columns

One of the powerful features of Flutter's layout system is that you can nest Rows and Columns to create more complex designs.

Example of Nested Rows and Columns:

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 { @override 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.

Important Properties to Note:

  • 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.

Conclusion

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.

Popular Tags

FlutterMobile DevelopmentUI Design

Share now!

Like & Bookmark!

Related Collections

  • Flutter Development: Build Beautiful Apps

    21/09/2024 | Flutter

Related Articles

  • Working with Forms and User Input in Flutter

    21/09/2024 | Flutter

  • Exploring Flutter for Web and Desktop

    21/09/2024 | Flutter

  • Setting Up Flutter Development Environment

    21/09/2024 | Flutter

  • Building Layouts with Flutter

    21/09/2024 | Flutter

  • Enhancing User Experience with Animations and Transitions in Flutter

    21/09/2024 | Flutter

  • Handling User Authentication in Flutter

    21/09/2024 | Flutter

  • Introduction to Flutter and Dart

    21/09/2024 | Flutter

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design