Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flutter, how to balance the children of the Wrap widget?

In Flutter, a Wrap widget attempts to place the child adjacent to the previous child in the main axis, and if there is not enough space to fit the child, it creates a new run adjacent to the existing children in the cross axis.

So, for example, if 3 children fit in a horizontal run, and we have 4 children, it will have 2 runs: The first with 3 children, and the second with 1 child.

Instead, after defining that number of runs, I would like it to "balance" the placement of these children, so that it occupies the same amount of runs, but the least amount of width.

In the above example, that would mean having 2 children in the first run, and 2 children in the second. But, of course, this should work to any number of children of any width.

How can I do that?

like image 840
MarcG Avatar asked Oct 28 '22 07:10

MarcG


1 Answers

As suggested by Rémi, I've recreated the Wrap widget with custom rules: The WrapSuper widget in https://pub.dev/packages/assorted_layout_widgets solves this.

It doesn't exactly guarantees the least amount of width, but instead the minimum raggedness, which is more precisely what I wanted.

like image 65
MarcG Avatar answered Nov 08 '22 16:11

MarcG