Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    ConstrainedBox

    Imposes additional size constraints on the child: "no narrower than", "no wider than", "no shorter than", "no taller than". Unlike SizedBox , it does not set the size but only sets the bounds within which the child chooses it itself.

    Type: single-child

    ConstrainedBox
      #constraints minWidth:100 minHeight:50
      ColoredBox color:#4f46e5
        Center
          Text "At least 100×50"

    Nested structures

    #constraints

    Parameter Type Default Description
    minWidth length 0 Minimum width
    maxWidth length unlimited Maximum width
    minHeight length 0 Minimum height
    maxHeight length unlimited Maximum height

    A length is a number (pixels) or a string with a unit: "100svh" , "50%" .

    The first positional word can pick a ready-made mode: expand (take everything, optionally with width: / height: ), tight , tightFor , loose :

    ConstrainedBox
      #constraints expand width:200
      Text "The width is fixed, the height is all that is available"

    There are no parameters directly on ConstrainedBox ( maxWidth:150 and the like) — only #constraints .

    Examples

    A column no wider than a readable width

    Center
      ConstrainedBox
        #constraints maxWidth:640
        Column spacing:8 crossAxisAlignment:start
          Text "Heading"
          Text "A long paragraph does not stretch wider than 640 pixels on a wide screen."

    A button no narrower than 120

    ConstrainedBox
      #constraints minWidth:120 minHeight:44
      ElevatedButton onPressed:"navigate:/home"
        Center
          Text "OK"

    Differences between the app and the site

    • The site reads only minWidth , maxWidth , minHeight , maxHeight . The named modes ( expand , tight , tightFor , loose ) have no effect on the site — no constraints appear.
    • If the parent sets a tight size ( SizedBox width:800 ), in the app it overrides the child's maxWidth , while on the site maxWidth stays in force — you get 640, not 800.

    Best practices

    • A readable text width on a page is a ConstrainedBox with maxWidth inside a Center ; on a narrow screen it simply shrinks to the window width.
    • Need an exact size — SizedBox ; need only bounds — ConstrainedBox .

    See also

    • SizedBox — a fixed size
    • Container — the same through #constraints , plus padding and decoration
    • AspectRatio — size from the aspect ratio