I have a widget tree as follows:
Widget example() {
return Column(
children: [
Row(
children: [
Expanded(
child: Container(
height: 100,
decoration: BoxDecoration(color: Colors.red),
),
),
Text('25.00%')
],
),
Row(
children: [
Expanded(
child: Container(
height: 100,
decoration: BoxDecoration(color: Colors.red),
),
),
Text('125.00%')
],
),
Row(
children: [
Expanded(
child: Container(
height: 100,
decoration: BoxDecoration(color: Colors.red),
),
),
Text('1250.00%')
],
),
],
);
}
How can I make all texts take the same horizontal space without using pixels in width so that all containers are aligned vertically?
I would like to set a fixed length for my percentages and the ones that are shorter are padded with spaces and the ones that are longer get cropped with the text elipsis.
Is there a clean build in method to do that?
2
Answers
the solution for all those issues is in the auto_size_text package, you can set a
maxLines
to set the maximum lines anAutoSizeText
so if you have multipleAutoSizeText
widgets, all will take the same horizontal space because however, your text is long, the package will resize it to fit in the specific lines ranges, otherwise, you can give it ellipsis style.Since your requirement is to set a fixed width for the percentages and ellipsize the text if it overflows, I’ll suggest that you wrap your
Text
widget too inside anExpanded
widget and useflex
property for both the expanded components of the row. I’m suggesting a ratio of 5:1 between both components of the row, represented byflex1
andflex2
respectively. However, you may set the flex as per your requirement.Secondly, I’ve used
overflow: TextOverflow.ellipsis
in all of theText
widgets that will take care of the ellipsis part of your requirement.I’ve also aligned the text to right but that’s totally up to u.
Following is the revised code:
Hope it helps!