Sobes.tech
Junior

What is a Container widget in Flutter and what are its main properties?

sobes.tech AI

Answer from AI

Container — a convenient widget that can be used to add padding, margin, borders, background, and transformations to other widgets.

Main properties:

  • alignment: Aligns the child widget within the container.
  • padding: Internal spacing between the container's border and its content.
  • color: Background color of the container.
  • decoration: Allows setting more complex decorations such as gradients, shadows, rounded corners, and background images. When using decoration, the color property cannot be used separately.
  • foregroundDecoration: Decoration drawn over the child widget.
  • width: Width of the container.
  • height: Height of the container.
  • constraints: Additional size constraints for the container.
  • margin: External spacing around the container.
  • transform: Applies a transformation to the container and its content.
  • child: The widget that is a child of the container.

Example usage:

Container(
  padding: const EdgeInsets.all(16.0), // Internal padding
  margin: const EdgeInsets.symmetric(vertical: 10.0), // External margin
  decoration: BoxDecoration( // Decoration with background color and rounded corners
    color: Colors.blue,
    borderRadius: BorderRadius.circular(10.0),
  ),
  child: const Text( // Child widget
    'Hello, Container!',
    style: TextStyle(color: Colors.white),
  ),
);
What is a Container widget in Flutter and what are… - sobes.tech