Sobes.tech
Intern

What is the purpose of the Container widget in Flutter?

sobes.tech AI

Answer from AI

The Container widget is used to add padding, borders, background, transformations, and other visual properties to a child widget. It can also be used to define the size of its child if it has no constraints by default.

Main uses:

  • Setting padding (inner spacing).
  • Setting margin (outer spacing).
  • Adding decoration (e.g., background, borders, shadows).
  • Defining width and height for explicit sizing.
  • Setting alignment to align the child within the Container.
  • Adding transform for transforming the child.
Container(
  padding: const EdgeInsets.all(8.0), // Inner padding
  margin: const EdgeInsets.symmetric(horizontal: 16.0), // Outer margin
  decoration: BoxDecoration( // Decoration
    color: Colors.blue, // Background color
    borderRadius: BorderRadius.circular(8.0), // Rounded corners
  ),
  width: 200.0, // Width
  height: 100.0, // Height
  alignment: Alignment.center, // Child alignment
  child: Text('Hello, Container!'), // Child widget
);
What is the purpose of the Container widget in… - sobes.tech