Autowrap widgets to new line in flutter

LayoutDartFlutter

Layout Problem Overview


I have 5 widgets with different sizes which would overflow if placed next to each other.

I am looking for a layout helper class that limits the widgets to the horizontal space and auto-wraps the widgets instead to a new line. First I was looking for a grid view but prefer instead a view which is independent since all elements have different widths. A multi line text field does actually already that, I just need the same approach with my widgets. Any ideas?

<Widget>[
    new RaisedButton(child: const Text('Foo')),
    new RaisedButton(child: const Text('Foo Bar')),
    new RaisedButton(child: const Text('Foo Bar Bas')),
    new RaisedButton(child: const Text('F')),
    new RaisedButton(child: const Text('B'))
]

Layout Solutions


Solution 1 - Layout

The Wrap widget is what you need:

return Wrap(
      children: <Widget>[
        new RaisedButton(child: const Text('Foo')),
        new RaisedButton(child: const Text('Foo Bar')),
        new RaisedButton(child: const Text('Foo Bar Bas')),
        new RaisedButton(child: const Text('F')),
        new RaisedButton(child: const Text('B'))
      ],
    );

Also you can add the properties runSpacing and spacing to your Wrap widget to give more space between your items in horizontal and vertical.

Wrap(
            runSpacing: 5.0,
            spacing: 5.0,

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel StephensView Question on Stackoverflow
Solution 1 - LayoutRomain RastelView Answer on Stackoverflow