Flex Widget and Attributes

Flex Widget and Attributes

ยท

3 min read

In Flutter, the Flex widget is used to create a flexible box that can hold multiple children and distribute available space along its main axis or cross axis. It's commonly used with its two primary children: Row and Column. The Flex widget itself is often not used directly, but its subclasses like Row and Column are used more frequently.

Here's a brief overview of the Flex widget and its common usage:

Flex Widget:

The Flex widget is a container that allows you to define a flexible space where children can be laid out along either the horizontal (main axis) or vertical (cross axis) direction. It takes a list of children, and you can specify the properties such as direction and mainAxisAlignment to control the layout.

Here are the attributes commonly used with Row and Column, which inherit from Flex:

Common Attributes:

  1. direction (Axis): Defines the main axis of the Row or Column. It can be either Axis.horizontal (default for Row) or Axis.vertical (default for Column).

  2. mainAxisAlignment (MainAxisAlignment): Aligns the children along the main axis. Common values include start, end, center, spaceBetween, and spaceAround.

  3. mainAxisSize (MainAxisSize): Determines the size of the Row or Column in the main axis. It can be MainAxisSize.max (default) to take as much space as possible or MainAxisSize.min to take only as much space as needed by the children.

  4. crossAxisAlignment (CrossAxisAlignment): Aligns the children along the cross axis. Common values include start, end, center, stretch, and baseline.

  5. textDirection (TextDirection): Determines the direction in which text flows within the Row or Column. It can be TextDirection.rtl (right-to-left) or TextDirection.ltr (left-to-right).

  6. verticalDirection (VerticalDirection): Determines the direction in which the children are placed along the cross axis. It can be VerticalDirection.down (default) or VerticalDirection.up.

Example:

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  crossAxisAlignment: CrossAxisAlignment.center,
  children:[
    // Children widgets here
  ],
)

In this example, a Row is defined with mainAxisAlignment set to MainAxisAlignment.spaceBetween and crossAxisAlignment set to CrossAxisAlignment.center. The children property is where you would list the widgets you want to include in the row.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flex Widget Example'),
        ),
        body: Flex(
          direction: Axis.horizontal,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children:[
            Container(
              width: 100.0,
              height: 500.0,
              color: Colors.red,
              child: const Text("Hello , This is First Row",
               style: TextStyle(
                          decoration: TextDecoration.underline,
                          decorationStyle: TextDecorationStyle.dashed,
                          color: Colors.amber,
                      ),
                    ),
            ),
            Container(
              width: 100.0,
              height: 500.0,
              color: Colors.green,
              child: const Text("Hello , This is Second Row",
              style: TextStyle(
                          decoration: TextDecoration.underline,
                          decorationStyle: TextDecorationStyle.dashed,
                          color: Colors.amber,
                     ),
                   ),
                 ),

            Container(
              width: 100.0,
              height: 500.0,
              color: Colors.blue,
              child: const Text("Hello , This is Third Row",
              style: TextStyle(
                    decoration: TextDecoration.underline,
                    decorationStyle: TextDecorationStyle.dashed,
                    color: Colors.amber,
                ),              
              ),
            ),
          ],
        ),
      ),
    );
  }
}

In this example, the Flex widget is used directly to create a horizontal arrangement of three colored containers.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย