textspan的文本不会显示出来的话,如果它溢出
That's how it looks when I have a WidgetSpan
and a TextSpan
, and the text inside the TextSpan
is too long:
problem
这里已经提出了一个类似的问题 TextSpan的文本不会显示它是否溢出,并以Widgetspan 放置,但是一个答案不适合我的问题。
我有两个text.rich()
widgets(每个主机一个widgetspan
和一个textspan
> textspan )展开了widget,都带有
,因为我希望它们的大小相同并尽可能多地占用空间。这就是为什么一排不是一个选项的原因,您(据我所知)不能并排放置两行,这发生了...flex:1
的扩展
使用行
仅使用witchoverflow.ellipsis
,如果文本太长:
target ui
< img src =“ https://i.sstatic.net/4ps2b.png” alt =“ target ui”>
这是我的代码:
Row(
children: [
Expanded(
flex: 1,
child: Text.rich(
TextSpan(children: [
WidgetSpan(
child: Container(
margin: const EdgeInsets.only(right: 4.0),
child: SvgPicture.asset("assets/icons/building.svg", color:
htLightGrey),
),
),
TextSpan(
text: incident.building.name,
style: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16.0,
color: htLightGrey, /*overflow: TextOverflow.ellipsis*/
),
),
]),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Expanded(
flex: 1,
child: incident.department != null
? Text.rich(
TextSpan(children: [
WidgetSpan(
child: Container(
margin: const EdgeInsets.only(right: 4.0),
child: SvgPicture.asset("assets/icons/department.svg",
color: htLightGrey),
),
),
TextSpan(
text: incident.department!.name,
style: const TextStyle(fontWeight: FontWeight.normal,
fontSize: 16.0, color: htLightGrey, overflow:
TextOverflow.ellipsis),
),
]),
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
: const SizedBox.shrink(),
)
],
)
That's how it looks when I have a WidgetSpan
and a TextSpan
, and the text inside the TextSpan
is too long:
problem
A similar question has already been asked here TextSpan's text doesn't show if it overflows and is placed after WidgetSpan, but the one answer isn't suitable for my problem.
I have two Text.rich()
widgets (which each host one WidgetSpan
and one TextSpan
) inside an Expanded
widget, both Expanded
with flex:1
, because I want them to be the same size and take as much space as they can. That's why a row is not an option, you can't (as far as I know) put two rows side by side, at this happens...
using rows
This is how it's supposed to look like, just with the TextOverflow.ellipsis
, if the text is too long:
target UI
Here is my code:
Row(
children: [
Expanded(
flex: 1,
child: Text.rich(
TextSpan(children: [
WidgetSpan(
child: Container(
margin: const EdgeInsets.only(right: 4.0),
child: SvgPicture.asset("assets/icons/building.svg", color:
htLightGrey),
),
),
TextSpan(
text: incident.building.name,
style: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16.0,
color: htLightGrey, /*overflow: TextOverflow.ellipsis*/
),
),
]),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Expanded(
flex: 1,
child: incident.department != null
? Text.rich(
TextSpan(children: [
WidgetSpan(
child: Container(
margin: const EdgeInsets.only(right: 4.0),
child: SvgPicture.asset("assets/icons/department.svg",
color: htLightGrey),
),
),
TextSpan(
text: incident.department!.name,
style: const TextStyle(fontWeight: FontWeight.normal,
fontSize: 16.0, color: htLightGrey, overflow:
TextOverflow.ellipsis),
),
]),
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
: const SizedBox.shrink(),
)
],
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要实现目标UI,请使用包装小部件而不是行小部件。在这里,当小部件溢出/超过它的内容将其分解为下一行,因此您将实现目标UI。
To achieve your target Ui, use the Wrap widget instead of the Row widget. Here, when the contents of the widget overflow/exceed it will break it to the next line, hence you'll achieve your target Ui.
当我们喜欢归档此UI时,我们将行用作父窗口小部件,这是完美的。然后,我们可以将行分为树的零件,左两个部分将获得相同的宽度和最大可用宽度。对于这种情况,小部件结构如下。
内部
text.rich
添加使用对齐:lastholderalignmign.middle,
onwidgetspan
以设置中间。您将获得
我正在使用额外的容器指向UI,只需从列中删除容器即可。另外,您可以使用行而不是富文本。
测试窗口
小部件有关 flutter布局
While we like to archive this UI, we are using Row as parent widget, which is perfect. Then we can split the row into tree part, left two part will get same and maximum available width. For this scenario, widget structure as follows.
And inside
Text.rich
add usealignment: PlaceholderAlignment.middle,
onWidgetSpan
to set the middle.And you will get
I am using extra container to point the UI, simply remove the container from the column. Also, you can use Row instead of Rich Text.
Test widget
More about flutter layout