Flutter Container Widget: Complete Guide with Examples (2026)
Last Updated: | Author: Deep Singh
The Container widget is one of the most used widgets in Flutter. It's a simple box that can hold a child, apply padding, margins, background colours, borders, gradients, shadows, and transforms — all in one place. If you're building any kind of UI, you'll reach for a Container almost immediately.
In this tutorial, you'll see how the Container behaves in different scenarios: with no child, with a child, with a fixed size, with a gradient, with an image, and more. Every example includes the source code and the output it produces.
Container Basics
A Container is a convenience widget that combines several common painting, positioning, and sizing widgets into one. It can:
- Contain a single child widget
- Apply padding and margin
- Set width and height
- Apply a background colour or decoration
- Apply a transform (rotate, scale, translate)
If the Container has no child, it expands to fill the area given by its parent. If it has a child, it wraps the child's size (unless you give it explicit dimensions).
The Container can only have one child. If you need to lay out multiple children, put a Row, Column, or Stack inside it and give that widget the children.
Empty Container
An empty Container just fills the space available to it. In this example, it fills the whole body of the Scaffold — which looks like a blank white screen.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My App',
home: ContainerWidget(),
// theme: ThemeData(primarySwatch: Colors.pink),
));
}
class ContainerWidget extends StatelessWidget {
const ContainerWidget({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Container Widget Tutorial'),
),
body: Container(),
);
}
}
Output:
Background Colour
To give the Container a background colour, just set the color property.
body: Container(
color: Colors.pinkAccent,
),
Output:
Note: You can't use both color and decoration with a colour inside it — Flutter will throw an error. Use one or the other.
Container with a Child
When you add a child to the Container, the Container wraps the child's size by default.
body: Container(
color: Colors.pinkAccent,
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
Output:
Full Width Container
To make the Container span the full screen width, use MediaQuery.of(context).size.width.
body: Container(
color: Colors.pinkAccent,
width: MediaQuery.of(context).size.width,
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
Output:
Aligning the Container
Wrap the Container in an Align widget to position it anywhere on the screen. Here it's pinned to the bottom.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.pinkAccent,
width: MediaQuery.of(context).size.width,
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Fixed Size Container
Give the Container explicit width and height to lock its size.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.pinkAccent,
width: 200,
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Transform (Rotation)
The transform property applies a matrix transformation. Here we rotate the Container 0.5 radians around the Z axis.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.pinkAccent,
width: 200,
transform: Matrix4.rotationZ(0.5),
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Linear Gradient
To use a gradient, you need to use decoration instead of color. A LinearGradient blends colours along a straight line.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 200,
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Colors.green, Colors.red, Colors.yellowAccent],
),
),
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Important: You cannot set both color and decoration. Pick one.
Radial Gradient
A RadialGradient radiates outward from the centre.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 200,
decoration: const BoxDecoration(
gradient: RadialGradient(
colors: [Colors.green, Colors.red, Colors.yellowAccent],
),
),
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Background Colour via BoxDecoration
You can also set the background colour inside BoxDecoration. This is useful when you need other decoration properties alongside the colour.
body: Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 200,
decoration: const BoxDecoration(
color: Colors.green,
),
child: ElevatedButton(
child: const Text('Click Me'),
onPressed: () {},
),
),
),
Output:
Image via BoxDecoration
You can set a background image using DecorationImage inside BoxDecoration.
body: Align(
alignment: Alignment.center,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
image: NetworkImage('https://www.drivespark.com/images..'),
),
),
),
),
Output:
Image with BoxFit
By default, the image is centred at its natural size. Use fit: BoxFit.cover to make it fill the Container, cropping as needed.
body: Align(
alignment: Alignment.center,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
image: NetworkImage('https://www.drivespark.com/images..'),
fit: BoxFit.cover,
),
),
),
),
Output:
Shadow and Image Filter
Use boxShadow to add a drop shadow, and colorFilter on the image to tint it.
body: Align(
alignment: Alignment.center,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
image: NetworkImage('https://www.drivespark.com/images..'),
fit: BoxFit.cover,
colorFilter: ColorFilter.linearToSrgbGamma(),
),
boxShadow: const [
BoxShadow(color: Colors.grey, offset: Offset(6.0, 6.0)),
],
),
),
),
Output:
Border Radius with Shadow
Add borderRadius to round the corners of the Container.
body: Align(
alignment: Alignment.center,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
image: NetworkImage('https://www.drivespark.com/images..'),
fit: BoxFit.cover,
colorFilter: ColorFilter.linearToSrgbGamma(),
),
boxShadow: const [
BoxShadow(color: Colors.grey, offset: Offset(6.0, 6.0)),
],
borderRadius: BorderRadius.circular(8),
),
),
),
Output:
Shape and Border Colour
Use shape: BoxShape.circle to make the Container circular, and Border.all() to add a border. Note that when using a circle shape, you can't also use borderRadius — pick one.
body: Align(
alignment: Alignment.center,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.green,
image: DecorationImage(
image: NetworkImage('https://www.drivespark.com/images..'),
fit: BoxFit.cover,
colorFilter: ColorFilter.linearToSrgbGamma(),
),
boxShadow: const [
BoxShadow(color: Colors.grey, offset: Offset(6.0, 6.0)),
],
border: Border.all(color: Colors.yellowAccent, width: 4),
shape: BoxShape.circle,
),
),
),
Output:
Wrapping Up
The Container widget is one of the most versatile in Flutter. It combines sizing, alignment, colour, decoration, and transforms into a single widget. Once you're comfortable with it, you'll use it in almost every screen you build.
A few things to remember:
- Don't use both
coloranddecoration. Flutter throws an assertion error if you do. Pick one. - Don't use both
borderRadiusandshape: BoxShape.circle. Same reason — the two are mutually exclusive. - Container can only hold one child. For multiple children, use a
Row,Column, orStackinside the Container. - Empty Container fills its parent. If you want it to size to zero, give it explicit dimensions or a child.
- Use
constwhere possible. Every example above usesconstfor the decoration and child widgets to help Flutter skip unnecessary rebuilds.
No comments:
Post a Comment