Flutter : Container Widget and Attributes

Flutter : Container Widget and Attributes

ยท

3 min read

The Container widget in Flutter provides a variety of attributes for customizing the appearance and layout of the contained widgets. Here are some key attributes of the Container widget:

  1. child (Widget):

    • The widget to be contained within the Container. It represents the content that will be displayed inside the box.
    Container(
      child: Text('Hello, Flutter!'),
    )
  1. alignment (AlignmentGeometry):

    • Determines the position of the child widget within the Container. It takes values like Alignment.topLeft, Alignment.center, etc.
    Container(
      alignment: Alignment.center,
      child: Text('Centered Text'),
    )
  1. padding (EdgeInsets):

    • Adds padding around the child widget within the Container.
    Container(
      padding: EdgeInsets.all(16.0),
      child: Text('Padded Text'),
    )
  1. margin (EdgeInsets):

    • Sets the external margin of the entire Container. It defines the space between this box and adjacent boxes.
    Container(
      margin: EdgeInsets.all(8.0),
      child: Text('Margin Around Container'),
    )
  1. color (Color):

    • Sets the background color of the Container.
    Container(
      color: Colors.blue,
      child: Text('Blue Background'),
    )
  1. width (double), height (double):

    • Specifies the width and height of the Container. These properties allow you to control the size of the box.
    Container(
      width: 150.0,
      height: 100.0,
      child: Text('Custom Size'),
    )
  1. decoration (BoxDecoration):

    • Provides advanced styling options for the Container, allowing you to set properties like border, borderRadius, boxShadow, etc.
    Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black),
        borderRadius: BorderRadius.circular(10.0),
      ),
      child: Text('Styled Container'),
    )
  1. constraints (BoxConstraints):

    • Specifies additional constraints on the Container size. For example, you can set maximum and minimum width/height.
    Container(
      constraints: BoxConstraints(
        minWidth: 100.0,
        minHeight: 50.0,
        maxWidth: 200.0,
        maxHeight: 150.0,
      ),
      child: Text('Constrained Container'),
    )

These attributes allow you to create flexible and styled layouts using the Container widget in Flutter. Combining these properties allows for precise control over the appearance and layout of UI elements within your application.

Certainly! Here's a merged example showcasing various attributes of the Container widget in Flutter:

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('Container Widget Example'),
        ),
        body: Center(
          child: Container(
            width: 200.0,
            height: 150.0,
            margin: EdgeInsets.all(10.0),
            padding: EdgeInsets.all(16.0),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(10.0),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey,
                  blurRadius: 5.0,
                  offset: Offset(2.0, 2.0),
                ),
              ],
            ),
            child: Text(
              'Styled Container',
              style: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 18.0,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

In this example, I've created a Container with various attributes:

  • width and height: Set the dimensions of the container.

  • margin: Adds external margin around the container.

  • padding: Adds internal padding within the container.

  • alignment: Aligns the child widget (text in this case) within the container.

  • decoration: Provides a styled appearance to the container, including color, borderRadius, and boxShadow.

  • child: The actual content of the container, which is a Text widget in this case.

Feel free to copy and paste this code into a new Flutter project to see how the Container widget with different attributes affects the appearance of the UI. You can also experiment with changing values to observe the real-time impact on the layout and style.

Did you find this article valuable?

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

ย