Learn/Core Widgets/Container & Decoration

Container & Decoration

The most versatile widget in Flutter

5 min~5 min read+30 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. EdgeInsets (Padding and Margin)
  2. 2. BoxDecoration Options
  3. 3. Spacing System For Cleaner UI
  4. 4. This Lesson In Simple Words
  5. 5. Your level focus
  6. 6. One practical habit
  7. 7. Deep Understanding Guide
  8. 8. Why this lesson matters
  9. 9. Study flow for better retention
  10. 10. Quick Glossary

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Container Widget

The Container widget is one of the most versatile building blocks in Flutter. It can add padding, margins, colors, borders, rounded corners, shadows, and gradients to any child widget. Think of it as a customizable box that wraps around other widgets.

dart
1// --- Container: A Box for Everything ---
2
3Container(
4 // Size
5 width: 200, // Box width
6 height: 100, // Box height
7
8 // Spacing inside the box
9 padding: EdgeInsets.all(16), // Space inside
10
11 // Spacing outside the box
12 margin: EdgeInsets.symmetric(
13 horizontal: 20, // Left & right margin
14 ),
15
16 // Visual styling
17 decoration: BoxDecoration(
18 color: Colors.white, // Background color
19 borderRadius: BorderRadius.circular(12), // Rounded corners
20 boxShadow: [ // Drop shadow
21 BoxShadow(
22 color: Colors.black26, // Shadow color
23 blurRadius: 10, // Shadow blur
24 offset: Offset(0, 4), // Shadow position
25 ),
26 ],
27 ),
28
29 // Content inside the container
30 child: Text('Hello'),
31)

EdgeInsets (Padding and Margin)

EdgeInsets is the class used to define spacing inside (padding) or outside (margin) a widget. Flutter provides several convenient constructors to cover common spacing patterns.

dart
1// --- EdgeInsets: Specify Spacing ---
2
3// Same spacing on all sides
4EdgeInsets.all(16) // 16px everywhere
5
6// Horizontal & Vertical
7EdgeInsets.symmetric(
8 horizontal: 20, // Left & Right = 20
9 vertical: 10, // Top & Bottom = 10
10)
11
12// Only specific sides
13EdgeInsets.only(
14 left: 10,
15 top: 20,
16)
17
18// All four sides separately
19EdgeInsets.fromLTRB(10, 20, 10, 20) // Left, Top, Right, Bottom

BoxDecoration Options

BoxDecoration provides multiple ways to style a Container. You can use solid colors, gradients, images, borders, and shadows. Here are the most common patterns.

dart
1// Option A: Solid color with rounded corners
2BoxDecoration(
3 color: Colors.blue,
4 borderRadius: BorderRadius.circular(16),
5 border: Border.all(color: Colors.grey, width: 2),
6 boxShadow: [
7 BoxShadow(
8 color: Colors.black26,
9 blurRadius: 10,
10 spreadRadius: 2,
11 offset: Offset(0, 4),
12 ),
13 ],
14)
15
16// Option B: Gradient background (cannot use 'color' with gradient)
17BoxDecoration(
18 gradient: LinearGradient(
19 colors: [Colors.blue, Colors.purple],
20 begin: Alignment.topLeft,
21 end: Alignment.bottomRight,
22 ),
23 borderRadius: BorderRadius.circular(16),
24)
25
26// Option C: Circular shape (cannot use 'borderRadius' with circle)
27BoxDecoration(
28 color: Colors.blue,
29 shape: BoxShape.circle,
30)
31
32// Option D: Background image
33BoxDecoration(
34 image: DecorationImage(
35 image: NetworkImage('https://example.com/image.jpg'),
36 fit: BoxFit.cover,
37 ),
38 borderRadius: BorderRadius.circular(16),
39)

Important

⚠️ Important

Do not use the `color` property of Container and the `color` inside BoxDecoration at the same time. Flutter will throw a runtime error. Use only one of them.

Spacing System For Cleaner UI

Instead of random values, use a small spacing scale like 4, 8, 12, 16, 24, 32.

Apply it consistently:

  • internal spacing via padding
  • external separation via margin or SizedBox
  • section grouping with larger gaps

This makes screens look intentional and professional even with simple widgets.

Container Consistency Checks

  • Padding values follow one spacing scale.
  • Border radius is consistent across similar components.
  • Shadow intensity is subtle and not overused.
  • Decoration choices match app theme and brand tone.

This Lesson In Simple Words

The most versatile widget in Flutter. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Sketch the widget tree first, then code from top to bottom.

Deep Understanding Guide

Why this lesson matters

Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. When a UI bug appears, inspect constraints and parent-child relations before changing random values.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Nesting too many widgets without extracting reusable pieces.
  • Ignoring constraints and guessing with random SizedBox heights.
  • Using one giant build method for unrelated UI sections.
  • Putting heavy calculations directly in build methods.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one card layout with Row/Column and rebuild it using Stack for comparison.
  • Split a large widget into 3 smaller widgets with clear responsibilities.
  • Use const constructors where possible and observe rebuild behavior.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What creates rounded corners?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.
  • build: The method that describes UI for the current state.
  • bloc: An event-to-state pattern that separates logic from UI.
  • const: A compile-time constant that can reduce rebuild work.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Container & Decoration" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Nesting too many widgets without extracting reusable pieces.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What creates rounded corners?

borderRadius in BoxDecoration creates rounded corners.

EdgeInsets.symmetric(horizontal: 20) adds padding to which sides?

horizontal affects left and right sides.

What makes a Container circular?

BoxShape.circle in BoxDecoration makes the container circular.

Can you use both color and gradient in BoxDecoration?

Using both color and gradient in BoxDecoration causes a runtime error. Choose one.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue