Divider Widget  and Attributes

Divider Widget and Attributes

ยท

2 min read

In the diverse landscape of Flutter widgets, the Divider widget stands out as a simple yet effective tool for creating visual separations between different sections of your user interface. Whether you're delineating items in a list or adding a touch of structure to your layout, the Divider is here to help. In this blog post, let's unravel the capabilities of the Divider widget and explore its attributes through practical examples.

The Divider widget in Flutter is designed to visually separate content within your UI, providing a clear distinction between different sections. It's commonly used to add horizontal or vertical lines between items, enhancing the overall layout and organization of your app.

Basic Implementation:

Let's start with a straightforward example to showcase the basic usage of the Divider widget:

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('Flutter Divider Example'),
        ),
        body: Column(
          children: [
            ListTile(
              title: Text('Item 1'),
            ),
            Divider(), // Simple horizontal divider
            ListTile(
              title: Text('Item 2'),
            ),
            Divider(), // Another horizontal divider
            ListTile(
              title: Text('Item 3'),
            ),
          ],
        ),
      ),
    );
  }
}

In this example, Divider is used between ListTile widgets to create a clear separation between items in a vertical list.

Key Attributes of Divider:

Now, let's explore some essential attributes that allow you to customize the appearance of the Divider widget:

1. color:

  • The color property allows you to set the color of the divider.

2. height:

  • Customize the height of the divider using the height property.

3. thickness:

  • The thickness property determines the thickness of the divider.

4. indent:

  • If you want to add an indentation before the divider, use the indent property.

5. endIndent:

  • Similarly, the endIndent property allows you to add an indentation after the divider.

Let's integrate these attributes into a comprehensive example:

Column(
  children: [
    ListTile(
      title: Text('Item 1'),
    ),
    Divider(
      color: Colors.blue,
      height: 20,
      thickness: 2,
      indent: 16,
      endIndent: 16,
    ),
    ListTile(
      title: Text('Item 2'),
    ),
    Divider(
      color: Colors.red,
      height: 30,
      thickness: 4,
      indent: 32,
      endIndent: 32,
    ),
    ListTile(
      title: Text('Item 3'),
    ),
  ],
)

In this enhanced example, we've customized the Divider with attributes such as a custom color, height, thickness, indent, and endIndent. Feel free to experiment with these attributes to achieve the desired visual outcome in your Flutter applications.

By incorporating the Divider widget and exploring its attributes, you can add a touch of structure and organization to your UI, making it more visually appealing and user-friendly.

Did you find this article valuable?

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

ย