TExcelTAD
Home › Flutter › Container & BoxDecoration

Flutter Container & BoxDecoration

Container is a convenience widget that combines sizing, spacing, alignment and decoration in one place. Think of it as a box you can give a size, margin, padding, background, border, rounded corners and shadow — similar to a <div> with CSS in web development.

Size, colour, padding and margin

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: Scaffold(
        body: Center(
          child: Container(            // Container has no const constructor
            width: 220,
            height: 120,
            margin: const EdgeInsets.all(16),   // space outside the box
            padding: const EdgeInsets.all(12),  // space inside the box
            color: Colors.amber,
            alignment: Alignment.bottomRight,
            child: const Text('Bottom right'),
          ),
        ),
      ),
    ));

EdgeInsets describes spacing on the four sides:

Constructor Meaning
EdgeInsets.all(16) 16 on every side
EdgeInsets.symmetric(horizontal: 24, vertical: 8) Left/right 24, top/bottom 8
EdgeInsets.only(left: 8, top: 4) Only the given sides
EdgeInsets.fromLTRB(8, 4, 8, 0) Left, top, right, bottom

BoxDecoration: borders, corners, shadows, gradients

For anything beyond a flat colour, pass a BoxDecoration to decoration:

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: TicketCard()))));

class TicketCard extends StatelessWidget {
  const TicketCard({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 280,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
          colors: [Color(0xFF7F00FF), Color(0xFFE100FF)],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderRadius: BorderRadius.circular(20),
        boxShadow: const [
          BoxShadow(color: Colors.black26, blurRadius: 12, offset: Offset(0, 6)),
        ],
      ),
      child: const Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('CONCERT PASS', style: TextStyle(color: Colors.white70, letterSpacing: 2)),
          SizedBox(height: 8),
          Text('Row F · Seat 12', style: TextStyle(color: Colors.white, fontSize: 24, fontWeight: FontWeight.bold)),
        ],
      ),
    );
  }
}

Warning: You cannot set both color and decoration on a Container — Flutter throws an assertion error. Put the colour inside the decoration: BoxDecoration(color: …).

Borders and circles

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: Scaffold(
        body: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Container(
                width: 80,
                height: 80,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.indigo, width: 3),
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              Container(
                width: 80,
                height: 80,
                decoration: const BoxDecoration(
                  color: Colors.pink,
                  shape: BoxShape.circle,
                ),
              ),
              Container(
                width: 80,
                height: 80,
                decoration: const BoxDecoration(
                  color: Color(0xFFE0F2F1),
                  border: Border(left: BorderSide(color: Colors.teal, width: 6)),
                ),
              ),
            ],
          ),
        ),
      ),
    ));

Colours in Flutter

Way to write it Example
Material palette Colors.blue, Colors.blue.shade100, Colors.blue[700]
Hex ARGB Color(0xFF2196F3) — the first two digits are opacity (FF = opaque)
With opacity Colors.black.withValues(alpha: 0.5)
From the theme Theme.of(context).colorScheme.primary

How Container decides its size

  • With width/height → it tries to be that size (within its parent's constraints).
  • With a child but no size → it shrinks to fit the child plus padding.
  • With no child and no size → it expands to fill the available space.

Tip: If you only need empty space, use SizedBox(height: 16). If you only need padding, use Padding. These single-purpose widgets are lighter and make your intent clearer than a Container.

Test yourself

What is the difference between margin and padding on a Container?

  1. They are identical
  2. margin is space outside the box; padding is space inside it
  3. padding is outside; margin is inside
  4. margin only works with a border

What happens if you set both color and decoration on one Container?

  1. decoration wins
  2. color wins
  3. An assertion error is thrown
  4. Both colours are blended

How do you make a Container perfectly round?

  1. borderRadius: BorderRadius.circular(360)
  2. BoxDecoration(shape: BoxShape.circle)
  3. Container(round: true)
  4. ClipOval is the only way