
Why Your Container Ignores width: Constraints Go Down, Sizes Go Up
- كاتب موثّق
- Flutter 3.x
محتويات المقال
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
width and height on a widget and it will be drawn at exactly that size, like a fixed frame in other UI toolkits.
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
The negotiation, step by step
A widget receives its from its parent: four numbers, a minimum and maximum width and a minimum and maximum height. It tells each child what constraints that child gets, which can differ per child, and asks what size it wants to be. It positions each child. A child never decides its own position. It reports its own size to its parent, inside the constraints it was given.
Tight, loose and unbounded
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. CenterandAligntake the tight constraints they receive and loosen them for their child.Unbounded: the maximum is infinite, like the height inside a vertical ListViewor the width aRowgives 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.
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:
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'), ), ], ); }}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')), // Center takes the tight width and loosens it: now 160 counts Center( child: 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.
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; }); }}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.
Container(width: 160, height: 48) | ||
|---|---|---|
ListView | ||
Center or Align | ||
Row | ||
Expanded inside a Row |
How to apply it in your project
Ask what the parent offers before changing a size When a widget ignores its widthorheight, the number is rarely the problem. Find the nearest parent that passes tight constraints: aListView,Expanded,SizedBox.expand, aColumnwithCrossAxisAlignment.stretch, or the root of the app.Loosen the constraints where you want freedom Insert CenterorAlignbetween the tight parent and the child. In aColumn, changecrossAxisAlignmentinstead of fighting it with sizes.Remember that SizedBox and ConstrainedBox only add limits They can narrow what the parent allows, never widen it. If the parent's constraints are tight, they lose like widthdid.Print or inspect, then read the docs Use LayoutBuilderor 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. widthandheightbecome extra constraints, and the parent's constraints always win when they conflict.Tight constraints ignore your numbers; CenterandAlignloosen 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.
التعليقات (0)