目前向Text
添加minLines
功能仍是一个待开发请求。您可以在这里进行跟踪。
暂时的解决方法是使用:
str = 'example';
Text(
str + '\n',
maxLines: 2,
)
你可以使用\n来设置最小行数(这是一个技巧)
这个问题仍然存在 Github: 添加minLines到Text (flutter issue)
Text(
'${product.name}\n\n', //equal minlines: 3
maxLines: 3,
overflow: TextOverflow.ellipsis,
style:
const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
Text(
"Some lines of text",
style: TextStyle(
fontSize: 14.0,
height: 1.5 //set height as you want
)
)
Row(
children: <Widget>[
Text('Begin'),
Spacer(), // Defaults to a flex of one.
Text('Middle'),
// Gives twice the space between Middle and End than Begin and Middle.
Spacer(flex: 2),
Text('End'),
],
)
您可以在官方文档中找到有关Spacer的更多详细信息。
这是我找到的最佳解决方案。
您可以在一个地方设置所有文本样式,并像这样获取它
static TextStyle subtitle2(Color color) =>
initStyle(color, 14.0, FontWeight.w600);
static TextStyle initStyle(
Color color,
double fontSize,
FontWeight fontWeight) =>
TextStyle(
height: 1.2,
fontSize: fontSize,
color: color,
fontWeight: fontWeight,
fontFamily: 'OpenSans');
不要忘记设置文本样式的高度属性
然后你可以像这样定义你的文本
TextStyle textStyle = AppTextStyle.headline6(AppColors.onBackground);
int lines = 3;
...
Container(
height: textStyle.height! * textStyle.fontSize! * lines,
child: Text('Title', style: textStyle))
对我来说完美无缺
试试这个,它会有效的。
Row(
children: [
Expanded(
flex: 1,
child: Text(
'Your long text goes here, you can add as much as you want',
maxLines: 2, /// you can change it on your requirements, or may be ignore it
),
)
],
)
maxLines
的解决方法是将Text
放置在一个Stack
中,与另一个(几乎)相同样式(除了透明)的Text
一起,该Text
具有确切的n
行。return Stack(
alignment: Alignment.center,
children: [
Text(
"\n\n\n",
style: Theme.of(context).textTheme.bodyMedium.copyWith(
color: Colors.transparent,
),
),
Text(
"Single Line of Text",
style: Theme.of(context).textTheme.bodyMedium,
),
],
);