【发布时间】:2020-05-24 09:51:48
【问题描述】:
有时当我使用颤振进行编码时,当我在文本小部件中使用长文本时,有时我会收到溢出消息,有时消息可以正常工作并且在多行中,为什么会发生这种情况?请向我解释如何避免这个问题。
【问题讨论】:
标签: flutter text overflow multiline longtext
有时当我使用颤振进行编码时,当我在文本小部件中使用长文本时,有时我会收到溢出消息,有时消息可以正常工作并且在多行中,为什么会发生这种情况?请向我解释如何避免这个问题。
【问题讨论】:
标签: flutter text overflow multiline longtext
Text Widgt 中有一些关键属性:
softWrap // if overflow then can wrap new line
overflow // if overflow the overed text style
maxLines // max lines
如果 Text Widgt 的父容器的宽度小于或等于设备宽度,则溢出的文本将换行为多行,或者如果文本太长,则 Text 将引发溢出错误
给父容器一个宽度
例如:
// overflow error
Container(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
给父容器固定宽度
// overflow will change to multiple lines, notice padding and margin's effect
Container(
width: 100,
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
或者让 Text 使用 Expended 或 Flexible 填充父容器
Expanded(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
// or
Flexible(
child: Column(
children: <Widget>[
Text("hellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohellohellohellohe" +
"llohellohellohellohellohellohello"),
Text("dd")
],
),
)
【讨论】: