Creating Flex Layouts in Flutter

Tutorial 3 of 5

1. Introduction

This tutorial aims to guide you through the process of creating flex layouts in Flutter. The Flex widget in Flutter allows us to create linear layouts (either horizontally or vertically) which can adapt to different screen sizes, creating a responsive design.

You will learn:
- Basic understanding of the Flex widget and its properties.
- How to create flexible layouts with the Flex widget.
- How to use the Expanded widget to control the flex factor of a child.

Prerequisites:
- Basic knowledge of Dart programming language.
- Basic understanding of Flutter and its widget tree structure.
- Flutter SDK setup on your local machine.

2. Step-by-Step Guide

In Flutter, the Flex widget allows you to create flexible layouts in either rows or columns. The Flex widget itself doesn't have a visual representation but is used to control the layouts of its children.

The main property of the Flex widget is the direction which determines if the layout is horizontal (Row) or vertical (Column).

The Expanded widget can be used within a Flex to control how much space a child should occupy in relation to others.

3. Code Examples

Let's create a simple horizontal flex layout.

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('Flex Layout')),
        body: Flex(
          direction: Axis.horizontal,
          children: [
            Expanded(
              flex: 2,
              child: Container(color: Colors.red),
            ),
            Expanded(
              flex: 1,
              child: Container(color: Colors.green),
            ),
            Expanded(
              flex: 1,
              child: Container(color: Colors.blue),
            ),
          ],
        ),
      ),
    );
  }
}

In this example, we have a Flex widget with Axis.horizontal direction, meaning it's a Row layout. We have 3 Expanded widgets as children. The flex property specifies how much space each child should occupy. The red box will take twice the space of the green and blue boxes.

4. Summary

In this tutorial, we've learned how to create flexible, responsive layouts using the Flex and Expanded widgets in Flutter. We've also learned how to control the space distribution among children widgets using the flex property.

As next steps, try creating complex layouts using nested Flex widgets. For additional resources, refer to the official Flutter documentation.

5. Practice Exercises

  1. Create a vertical flex layout with 3 children with the ratio 1:2:3.
  2. Create a layout with nested flex layouts.
  3. Create a layout which changes from Row to Column orientation based on screen size.

Solutions:

  1. Use Axis.vertical and set flex to 1, 2, and 3 for the three Expanded widgets.
  2. Add a Flex widget as a child of another Flex widget. Experiment with different flex values.
  3. Use the MediaQuery widget to get the screen size, and set the direction of the Flex widget accordingly.

Keep practicing and experimenting with different layouts to get a feel for how the Flex and Expanded widgets work.