Migration to Flutter Guide
Discover our battle-tested 21-step framework for a smooth and successful migration to Flutter!
Home
Glossary

Container in Flutter

What is Container in Flutter?

Container is a basic layout widget in Flutter app development used to wrap a single child and apply size, spacing, alignment, and decoration.

It is a convenience widget that combines several lower-level widgets into one. Developers often use it to quickly style UI elements like cards, tiles, or sections.

Why does Container matter in Flutter app development?

Container simplifies UI code.

Instead of nesting multiple widgets for padding, alignment, and background, you can express all of this in one place, which improves readability for simple layouts.

How does Container work?

Container does not decide its size on its own.

Its size is determined by:

  • explicit width and height
  • constraints from the parent widget
  • the size of its child (if no size is provided)

Internally, Container is a combination of widgets such as Padding, Align, DecoratedBox, and ConstrainedBox.

Container in Flutter: example

Container(
  width: 120,
  height: 80,
  padding: const EdgeInsets.all(8),
  alignment: Alignment.center,
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8),
  ),
  child: const Text('Hello', style: TextStyle(color: Colors.white)),
)

Container in Flutter: Key characteristics

  • Supports only one child.
  • Can apply margin, padding, alignment, and decoration.
  • Color must be defined inside BoxDecoration if decoration is used.

Common mistakes when using Container in Flutter

A frequent misconception is that height is ignored inside Column or ListView.

This is not true – a fixed height (e.g. height: 100) works correctly.

The real issue is using height: double.infinity or Expanded inside scrollable widgets, which causes a runtime layout error due to unbounded constraints.

Another mistake is overusing Container instead of simpler widgets.

When to use Container?

Container is a versatile widget that combines layout, positioning, and styling in one. It is particularly useful when you need to apply multiple visual or structural properties simultaneously, such as a background color, padding, margin, alignment, or a border. For example, you might wrap a button in a Container to give it a colored background, rounded corners, and some internal spacing.

Container is also helpful when building composite widgets that combine decoration and size constraints in a single parent, making the code more readable and centralized.

Container works best when the parent constraints are well-defined, and the widget needs to express both visual styling and layout behavior together. It is commonly used for cards, highlighted sections, or placeholders in a UI where multiple properties need to be adjusted in one place.

When not to use Container?

Container should be avoided when you only need a single property, because using it in such cases adds unnecessary complexity and overhead. For instance:

  • If you only need spacing, using Padding is clearer and can be declared as const.
  • If you only need to set a fixed width or height, SizedBox is more efficient.
  • If you only need alignment, Align is simpler and more explicit.

Using these dedicated widgets not only improves readability, but also allows Flutter to optimize rebuilds and memory usage more effectively. In situations where the layout is simple, sticking to specialized widgets avoids overuse of Container and helps maintain a clean, performant widget tree.

Alternatives to Container

For cleaner and more efficient layouts, prefer specialized widgets and use Container only when its flexibility is actually needed.

Learn more

Design system Flutter

Building a Design System in a Large Flutter App

Flutter is known for its seamless UI/UX features and robust design elements. It allowed us to deliver a unique customer experience in the “CA24 Mobile” banking app. This article shares some of the learnings and pain points behind implementing it in this large project.

Advantages of design system

Benefits of a Design System: Why Your Digital Product Needs One

Building a new digital product, such as a web or mobile application, is complex. You need to map user stories - create a simple description of features - then apply them to the developers' scope of work and the app’s design. Discover how a design system can unleash efficiency, consistency, and scalability in your digital product.