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
widthandheightfor explicit sizing. - Setting
alignmentto align the child within theContainer. - Adding
transformfor 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
);