When using the Expanded or Flexible widget to avoid render flex overflow caused by text content within a Row widget, the space between applied to separate the texts is lost.
This is a custom widget that I created to use within the application, here is the code:
class CustomInfoRow extends StatelessWidget {
final String label;
final String value;
final bool hasDivider;
const CustomInfoRow({
super.key,
required this.label,
required this.value,
required this.hasDivider,
});
@override
Widget build(BuildContext context) {
final textStyle =
Theme.of(context).textTheme.bodyLarge?.copyWith(fontSize: 18.0);
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
SharedUtils.translate(context, label),
style: textStyle,
),
),
Expanded(
child: Text(
SharedUtils.translate(context, value),
style: textStyle,
),
),
],
),
if (hasDivider) const Divider(),
],
);
}
}
When I use Flexible widget, it looks like this:
When I use Expanded widget, it looks like this:
This is how I want it looks like:
Please, I appreciate help to solve this situation.
I want to achieve what is described in the last image
2
Answers
You just need to remove the
Flexible
from your Text on the right.Result:
Code:
Certainly, when addressing the issue raised in the Stack Overflow question regarding the loss of space between texts when using
Expanded
orFlexible
widgets within aRow
, there are several approaches that can be taken to resolve this layout challenge.Use
SizedBox
for Explicit Spacing:One straightforward solution is to introduce
SizedBox
widgets between the text elements in theRow
to create explicit space.SizedBox
can have a fixed width or be defined by awidthFactor
to determine its width relative to available space.Utilize
Flexible
with Appropriateflex
Values:The
Flexible
widget offers more flexibility thanExpanded
as it allows you to specify the flexibility of a child with aflex
attribute. By adjusting theflex
value, you can better control how the space is distributed among the children of theRow
.Adjust
MainAxisAlignment
for Even Distribution:If you want the space to be evenly distributed not only between the text elements but also from the elements to the edges of the
Row
, you can useMainAxisAlignment.spaceAround
orMainAxisAlignment.spaceEvenly
. These alignment properties will create equal spaces in the direction of the main axis.Remember to adjust the size of the space and the
flex
values according to the specific layout requirements and design intentions. These suggestions should help you address the issue and achieve the desired layout appearance.