颤动右溢出行中的两个文本
我想将 2 个文本放在一行中。我执行以下操作:
Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
Flexible(
child: Text(text1,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left)),
Flexible(
child: Text(text2,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left)),
]),
这里我显示了溢出和实际的预期结果:
即使有可用空间,任何文本都不能扩展超过行总长度的一半。尽管 Text1 和 Text2 的长度可能不同,如何实现我期望的结果?
I want to put 2 Text in one Row. I do the following:
Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
Flexible(
child: Text(text1,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left)),
Flexible(
child: Text(text2,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.left)),
]),
Here I have shown the result expected with overflows and actual:
None of the Text can expand more than half the total length of the Row, even if there is free space. How to implement the result I expect, despite the fact that the length of Text1 and Text2 may be different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
Flexible
中计算的flex
值,根据字符串的长度来分割每个字符串可用的区域。对于非等宽字体,结果并不总是最佳的,但不知何故,您必须指定文本应占用多少空间。您可以使用Expanded
来填充剩余的可用空间,但只有当您有一个固定宽度的项目并在另一个项目上使用Expanded
时,它才有效。试试这个(在
Container
上设置宽度和红色仅用于演示):You can use the length of the strings to split the area available to each one using calculated
flex
values inFlexible
. The result will not always be optimal with non monospaced fonts, but somehow you have to assign how many space should the texts occupy. You could useExpanded
to fill the remaining available space, but it only works if you have a fixed width item and useExpanded
on the other one.Try this (setting width on the
Container
and red color is only for demonstration):为行添加 MainAxisAlignment.spaceBetween ,它将在文本之间放置空格,使它们左右对齐。
add MainAxisAlignment.spaceBetween for row it will put space between the text aligning them to left and right.
我的要求有点不同。就我而言,第一个文本是标签,通常要求最小长度为 30,最大长度为 50(以百分比表示)。因此创建了一种根据文本长度计算弹性因子的方法。您可以根据需要更改此阈值。
要了解有关 flex 属性的更多信息 -> https://api.flutter.dev/flutter/widgets/Flexible/flex。 html
My requirement was little bit different. In my my case the first text is label which generally requires minimum length 30 and maximum length 50 in percentage. So created a method which calculates flex factor according to texts length. You can change this threshold as per you requirement.
To know more about the flex property -> https://api.flutter.dev/flutter/widgets/Flexible/flex.html