DEV Community

raman04-byte
raman04-byte

Posted on • Originally published at youtu.be

Mastering Flutter Layouts: Building Beautiful UIs with Rows and Columns

Flutter has emerged as a leading framework for mobile app development, renowned for its flexibility and performance. When it comes to creating stunning Flutter applications, mastering layout design is a key skill. In this blog, we'll dive into the world of rows and columns, two fundamental widgets that form the backbone of flexible and responsive UI designs in Flutter.
Understanding Rows and Columns:

  1. Before we delve into the technicalities, let's get acquainted with rows and columns. In Flutter, rows and columns are essential layout widgets that enable us to arrange other widgets horizontally and vertically, respectively. These widgets play a crucial role in organizing the user interface, allowing developers to create dynamic and aesthetically pleasing app layouts. Using Row for Horizontal Arrangement:

Let's start with a practical example of Row, which facilitates horizontal arrangements. In the code snippet below, we use the Row widget to horizontally arrange three colored containers:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Row Example'),
        ),
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Container(
                width: 100,
                height: 100,
                color: Colors.red,
              ),
              Container(
                width: 100,
                height: 100,
                color: Colors.green,
              ),
              Container(
                width: 100,
                height: 100,
                color: Colors.blue,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

By running this code in a Flutter project, you'll witness how Row and its mainAxisAlignment property, set to spaceEvenly, evenly distribute the containers along the main axis (horizontal axis in this case).
Using Column for Vertical Arrangement:

Next, let's explore the power of Column creating vertical arrangements. Consider the following code snippet, where we utilize the Column widget to vertically arrange three colored containers:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Column Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Container(
                width: 100,
                height: 100,
                color: Colors.yellow,
              ),
              Container(
                width: 100,
                height: 100,
                color: Colors.orange,
              ),
              Container(
                width: 100,
                height: 100,
                color: Colors.purple,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

As you execute this code in a Flutter project, observe how the Column widget, combined with mainAxisAlignment set to spaceEvenly, ensures equal spacing between the containers along the main axis (vertical axis in this case).
Creating a Simple Login Screen Layout:

Now, let's combine rows and columns to build a simple login screen layout. In this example, we'll place the logo, email TextFormField, password TextFormField, and login ElevatedButton vertically using the Column widget:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LoginPage(),
    );
  }
}

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 20.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            FlutterLogo(
              size: 100.0,
            ),
            SizedBox(height: 20.0),
            TextFormField(
              decoration: InputDecoration(
                labelText: 'Email',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 20.0),
            TextFormField(
              decoration: InputDecoration(
                labelText: 'Password',
                border: OutlineInputBorder(),
              ),
              obscureText: true,
            ),
            SizedBox(height: 30.0),
            ElevatedButton(
              onPressed: () {
                // Login button action
              },
              child: Text('Login'),
            ),
          ],
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Running this code in a Flutter project will result in a clean and straightforward login screen layout. The SizedBox widgets add spacing between the elements, while the Expanded widget is not required since the widgets inside the Column automatically occupy the available vertical space.
Flexibility and Responsiveness:

Building responsive layouts is crucial for delivering a consistent user experience across various devices. We'll explore how to make your UI flexible, dynamically adjusting its components based on the available screen space. This knowledge is invaluable for creating apps that look great on different smartphones and tablets.

Conclusion:
Mastering rows and columns in Flutter is an essential skill for any mobile app developer. Understanding how to create flexible and responsive layouts empowers you to design visually appealing user interfaces that impress users and elevate your app's user experience. By following the techniques covered in this blog and experimenting with different layouts, you'll become proficient in crafting impressive UI designs using rows and columns in Flutter. So, roll up your sleeves, start coding, and let your creativity flow as you build remarkable UIs! Happy coding!
You can check the video here: https://youtu.be/jYvuLexT044 (in Hindi)

Top comments (0)