A Container widget helps you group and style parts of your app's screen. It can add space, color, borders, and size to its child widget.
Container widget in Flutter
Container( width: double?, height: double?, padding: EdgeInsets?, margin: EdgeInsets?, decoration: BoxDecoration?, child: Widget? )
width and height set the size of the container.
padding adds space inside the container around the child.
Container(
color: Colors.blue,
child: Text('Hello')
)Container( width: 100, height: 100, margin: EdgeInsets.all(10), decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(8), ), child: Icon(Icons.star, color: Colors.white), )
This app shows a green square container with rounded corners and a black border. Inside, there is white text centered with padding around it. The container also has margin space outside it.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Container Example')), body: Center( child: Container( width: 150, height: 150, padding: const EdgeInsets.all(20), margin: const EdgeInsets.all(30), decoration: BoxDecoration( color: Colors.green, borderRadius: BorderRadius.circular(15), border: Border.all(color: Colors.black, width: 3), ), child: const Center( child: Text( 'Hi Flutter!', style: TextStyle(color: Colors.white, fontSize: 18), textAlign: TextAlign.center, ), ), ), ), ), ); } }
If you only set color on Container, it applies directly. But if you use decoration, set color inside BoxDecoration.
Container tries to be as big as possible if no size is set. Use width and height to control size.
Use padding to add space inside the container, and margin to add space outside it.
Container is a box that holds and styles one child widget.
You can control size, color, padding, margin, and borders with Container.
It helps organize and decorate your app's layout easily.
Practice
Container widget in Flutter?Solution
Step 1: Understand Container's role
The Container widget is designed to hold one child widget and apply styling like size, color, padding, and margin.Step 2: Compare with other options
Navigation, data fetching, and input handling are done by other widgets or classes, not Container.Final Answer:
To hold and style a single child widget with size, color, and padding -> Option DQuick Check:
Container = styling box for one child [OK]
- Confusing Container with navigation widgets
- Thinking Container handles user input
- Assuming Container manages data fetching
Solution
Step 1: Recall Container size properties
Container useswidthandheightproperties inside aBoxConstraintsviaconstraintsor directly inwidthandheightparameters.Step 2: Check syntax correctness
Width and height expect double values, so 100.0 is correct. Other options use invalid syntax: nosizeproperty,constraints: BoxConstraints(width: 100, height: 100)is wrong as BoxConstraints usesminWidth/maxWidth, nodimensions.Final Answer:
Container(width: 100.0, height: 100.0) -> Option CQuick Check:
Width and height use double values [OK]
- Using non-existent 'size' or 'dimensions' properties
- Using BoxConstraints with invalid 'width'/'height' parameters
- Confusing Container size with padding
Container(
width: 100.0,
height: 100.0,
color: Colors.blue,
child: Container(
color: Colors.red,
width: 50,
height: 50,
),
)Solution
Step 1: Understand nested Containers
The outer Container has a blue color and contains a smaller inner Container colored red with fixed size 50x50.Step 2: Visualize layering
The outer Container's blue color fills its area. The inner Container is smaller and red, so it appears as a red box inside the blue background.Final Answer:
Blue background with a smaller red box inside -> Option AQuick Check:
Outer color shows behind inner smaller Container [OK]
- Thinking inner Container replaces outer color
- Assuming colors blend automatically
- Ignoring size difference between Containers
Container(
padding: EdgeInsets.all(10),
margin: EdgeInsets.symmetric(horizontal: 20),
color: Colors.green,
child: Text('Hello'),
width: 100,
height: 50,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
)Solution
Step 1: Check Container property rules
Container'scoloranddecorationcannot be used together because color should be inside BoxDecoration if decoration is used.Step 2: Verify other properties
EdgeInsets.symmetric with only horizontal is valid, Text can be child, width and height are direct Container properties, not inside decoration.Final Answer:
Cannot use both color and decoration properties together -> Option BQuick Check:
color and decoration are mutually exclusive [OK]
- Using color and decoration simultaneously
- Misunderstanding EdgeInsets.symmetric usage
- Placing width/height inside decoration
Solution
Step 1: Set fixed size and padding
Width and height are set directly on Container. Padding is set with EdgeInsets.all(16) to add space inside the border.Step 2: Define border and rounded corners in decoration
Use BoxDecoration with border: Border.all(color: Colors.red, width: 3) for red border 3 pixels thick, and borderRadius.circular(12) for rounded corners.Step 3: Check other options
Container( width: 150, height: 150, margin: EdgeInsets.all(16), decoration: BoxDecoration( border: Border.all(color: Colors.red, width: 3), borderRadius: BorderRadius.circular(12), ), child: Text('Styled Box'), ) uses margin instead of padding, which adds space outside border. Container( width: 150, height: 150, padding: EdgeInsets.all(16), color: Colors.red, borderRadius: BorderRadius.circular(12), child: Text('Styled Box'), ) incorrectly uses color and borderRadius outside decoration. Container( width: 150, height: 150, padding: EdgeInsets.all(16), decoration: BoxDecoration( border: Border.all(color: Colors.red), borderRadius: BorderRadius.circular(12), ), child: Text('Styled Box'), ) misses border width, default is 1 pixel.Final Answer:
Container with width, height, padding, and BoxDecoration with red border 3px and 12px radius -> Option AQuick Check:
Border and radius go inside decoration; padding adds inner space [OK]
- Using margin instead of padding for inner space
- Setting color and borderRadius outside decoration
- Forgetting border width in Border.all
Ready to prove it?
Pass the test and this pattern moves unknown
