الانتقال إلى المحتوى
تطبيقات الموبايلFlutter· 8 دقائق للقراءة

Why Your Container Ignores width: Constraints Go Down, Sizes Go Up

A width is a request, not an order: the parent's constraints always win, so fix the parent, not the number.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

The checkout screen has a teal Save button built from a Container with width: 160. On the device it stretches from edge to edge. The developer tries 120, then 80, then wraps it in a SizedBox(width: 160). Nothing changes, because none of those numbers was ever in charge of the final size. The ListView it sits in was.

The common misconception

The belief: set width and height on a widget and it will be drawn at exactly that size, like a fixed frame in other UI toolkits.

It's an easy one to pick up, because it works most of the time. The property is literally called width. Put a Container(width: 160) inside a Center, a Row or a Scaffold body and you get 160 pixels, which is what most tutorials show. The surprise comes only under certain parents, so it looks like a random bug rather than a rule.

What actually happens

Flutter's docs sum up the layout rule in one line: constraints go down, sizes go up, parent sets position.

The negotiation, step by step

Layout is a single pass down the tree and back up:

  1. A widget receives its from its parent: four numbers, a minimum and maximum width and a minimum and maximum height.
  2. It tells each child what constraints that child gets, which can differ per child, and asks what size it wants to be.
  3. It positions each child. A child never decides its own position.
  4. It reports its own size to its parent, inside the constraints it was given.

So a widget can choose its size only within what its parent allows. The docs put it bluntly: a widget usually can't have any size it wants.

Tight, loose and unbounded

Three kinds of constraints explain almost every surprise:

  • Tight: the minimum equals the maximum, so there's exactly one allowed size. The root of your app gets tight constraints equal to the screen.
  • Loose: the minimum is zero, so the child may be any size up to the maximum. Center and Align take the tight constraints they receive and loosen them for their child.
  • Unbounded: the maximum is infinite, like the height inside a vertical ListView or the width a Row gives its children.

What width really does

Container turns width and height into extra constraints, roughly BoxConstraints.tightFor(width: 160), and combines them with what the parent passed. The parent's limits always win. As the docs say about ConstrainedBox, it only imposes additional constraints on top of those it receives. Under , the extra ones have nowhere to go, and the Container takes the parent's size. The docs' second layout example shows it at the root: a Container(width: 100, height: 100) given straight to the app fills the whole screen, because the screen passes tight constraints.

That's the Save button. A vertical ListView gives each child a tight width equal to its own width and an unbounded height. The 48 for the height survives; the 160 for the width doesn't stand a chance:

Dart checkout_form.dart
import 'package:flutter/material.dart';class CheckoutForm extends StatelessWidget {  const CheckoutForm({super.key});  @override  Widget build(BuildContext context) {    return ListView(      padding: const EdgeInsets.all(16),      children: [        const TextField(decoration: InputDecoration(labelText: 'Name')),        // ListView forces a tight width: 160 is ignored, the button fills it        Container(          width: 160,          height: 48,          alignment: Alignment.center,          decoration: const BoxDecoration(color: Colors.teal),          child: const Text('Save'),        ),      ],    );  }}

Center accepts the tight width itself, fills the row, and passes its child loose constraints from zero up to that width. The Container's 160 now fits inside them, and Center places the 160-pixel button in the middle (Align does the same with any alignment you choose). Notice the fix didn't touch the number: it changed what the parent offers.

You don't have to guess what a parent offers. Wrap the child in a small helper and print it:

Dart show_constraints.dart
import 'package:flutter/material.dart';class ShowConstraints extends StatelessWidget {  const ShowConstraints({super.key, required this.child});  final Widget child;  @override  Widget build(BuildContext context) {    return LayoutBuilder(builder: (context, constraints) {      debugPrint('$constraints');      return child;    });  }}

In a widget test on the default 800-pixel-wide surface, the button inside the padded ListView prints BoxConstraints(w=768.0, 0.0<=h<=Infinity) and measures 768×48. Inside the Center, it prints BoxConstraints(0.0<=w<=768.0, 0.0<=h<=Infinity) and measures 160×48. Same widget, same numbers, different parent.

Parent of Container(width: 160, height: 48)Constraints it passesFinal size
The root of the appTight: exactly the screenThe whole screen
A vertical ListViewTight width, unbounded heightFull width × 48
Center or AlignLoose: zero up to the parent's size160 × 48
RowUnbounded width160 × 48
Expanded inside a RowTight width: its share of the rowIts share × 48

How to apply it in your project

  1. Ask what the parent offers before changing a sizeWhen a widget ignores its width or height, the number is rarely the problem. Find the nearest parent that passes tight constraints: a ListView, Expanded, SizedBox.expand, a Column with CrossAxisAlignment.stretch, or the root of the app.
  2. Loosen the constraints where you want freedomInsert Center or Align between the tight parent and the child. In a Column, change crossAxisAlignment instead of fighting it with sizes.
  3. Remember that SizedBox and ConstrainedBox only add limitsThey can narrow what the parent allows, never widen it. If the parent's constraints are tight, they lose like width did.
  4. Print or inspect, then read the docsUse LayoutBuilder or the DevTools inspector to see the real constraints. For anything else, read the widget's docs: Flutter's own guide warns that you can't know how a widget lays out just from its name.

Key takeaways

  • Constraints go down, sizes go up, and the parent sets the position: a widget only picks a size within its limits.
  • width and height become extra constraints, and the parent's constraints always win when they conflict.
  • Tight constraints ignore your numbers; Center and Align loosen them so the child's size counts.
  • Unbounded constraints break widgets that want to fill space, so give scrollables a bounded parent.
  • Debug sizes by printing or inspecting constraints, not by trying new numbers.

📚 المصادر

  1. Understanding constraints — Flutter docs
  2. BoxConstraints class — Flutter API
  3. Container class — Flutter API
  4. Layouts in Flutter — Flutter docs

التعليقات (0)