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

    Flex

    A linear layout whose axis is a parameter. Row and Column are a Flex with a fixed axis; Flex is for when the axis is decided by an expression: "a row on a wide screen, a column on a narrow one" — one element with one condition instead of the whole section written twice.

    Type: multi-child

    Flex direction:horizontal spacing:12
      Text "Left"
      Text "Right"

    The other parameters are the same as for Row and Column , including the short synonyms, so a section moved to Flex is written with the same words.

    Parameters

    Parameter Type Default Description
    direction string vertical Axis: horizontal or vertical . Without the parameter — a column
    mainAxisAlignment string start Alignment along the main axis: start , end , center , spaceBetween , spaceAround , spaceEvenly
    alignment string start Synonym of mainAxisAlignment (read when that one is absent)
    mainAxisSize string max Size along the main axis: min — fit the children, max — all available space
    size string max Synonym of mainAxisSize
    crossAxisAlignment string center Alignment along the cross axis: start , end , center , stretch , baseline
    crossAlignment string center Synonym of crossAxisAlignment
    spacing length 0 Gap between children: a number (px) or a length in viewport units ( "2vw" ). Percent is not supported
    textDirection string — ltr or rtl : which way the horizontal axis runs
    textBaseline string — alphabetic or ideographic . With crossAxisAlignment:baseline and no value, alphabetic is used

    Examples

    Axis by the box width

    The width comes from LayoutBuilder ; on the site, a condition on it becomes a @container rule and is correct from the very first frame.

    LayoutBuilder maxWidth:@w
      Flex direction:{w > 480 ? "horizontal" : "vertical"} spacing:16 crossAxisAlignment:start
        ColoredBox color:0xFFE0E7FF
          Padding
            #padding all 16
            Text "Section text"
        ColoredBox color:0xFFC7D2FE
          Padding
            #padding all 16
            Text "Illustration"

    Axis by the screen size class

    For page-level bands, take the axis from $theme.sizeClass — on the site this is a @media rule (see responsive layout ).

    Flex direction:{$theme.sizeClass == "compact" ? "vertical" : "horizontal"} spacing:24
      Text "Heading"
      Text "Description"

    Baseline alignment

    Flex direction:horizontal crossAxisAlignment:baseline textBaseline:alphabetic spacing:8
      Text "128"
        #style fontSize:40 fontWeight:bold
      Text "steps"

    Differences between the app and the site

    • On the site Flex is display: flex with flex-direction: row or column .
    • An axis computed from the LayoutBuilder width goes into @container , one from $theme.sizeClass — into @media ; spacing is carried over the same way.
    • An axis from ordinary state (an @var changed by a button) does not switch on the site : it is taken from the initial value when the page is built and does not change afterwards. In the app it changes. To make the axis change on the site, compute it from the width or from the size class.

    See also