Home > Mobile >  Flutter-equivalent of CSS max-content and min-content
Flutter-equivalent of CSS max-content and min-content

Time:11-02

Sometimes, after wrapping a list of widgets inside a Column or a ListView, the widget takes the whole width or height of this column. However, I simply wish it took the height or width of its contents, but so far I've only been able to find either very complicated ways of doing this or ways of giving, for example, buttons and texts fixed width or height.

In CSS, there's min-content and max-content for doing this sort of stuff. Is there an equivalent in Flutter?

CodePudding user response:

Look at the Flexible widget. Unlike Expanded, it tells the widgets to take up as much space as they need. They can fill the whole space if they need to, but otherwise they only take up the space their contents do.

See also this comparison of Flexible and Expanded: Flutter: Expanded vs Flexible.

CodePudding user response:

Partial answer here: sometimes, putting the stretched widget inside a Row or a Column can work. And there are also some specific widgets for this, e.g. ConstrainedBox.

  • Related