3 items have to be displayed in a row: image and a description below, so it's basically a Row
of Column
s. Description size varies and can either exceed or be smaller than the image's width. These images has to be laid out evenly with the description multilined if needed.
So the naive code looks roughly like this:
@override
Widget build(BuildContext context) => Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
for (final item in items)
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset(item.depiction),
Text(
item.name,
textAlign: TextAlign.center,
),
],
)
],
);
The problem is that the Column
is sized by width from the text's size, and if it exceeds the image's size, then the images in a Row
aren't spaced evenly. The solution I see is to constrain the Column
width to the width of Image
, using, for example, some kind of Builder
widget, but it does not seem right at all.
What I tried is wrapping all the Row
's children with Expanded
, but then spaceAround
does not have any effect, as all the children get sized to the 1/3 of the Row
, and this spacing is vital, where the free space should have the value of row.length - 3 * image.size
(pseudocode to give a general idea). Wrapping Column
with Flexible
and setting stretch
cross axis alignment to the same Column
gives the same effect - Column
sizes to the 1/3 of the Row
length.
What's the proper way to constraint the Text
width to have maxWidth
of image's width?
Image:
In the third Row
it works properly, as the Text
width is less than image's.
In the second Row
Text
's width is greater than image's width, so the Column
is sized by the one-line text width and the image's layout is not equal to the previous one.
The first Row
is how I want the second Row
to be rendered, in this example it's just the separated (\n
) description which results in the visual effect I seek for.