如何在Text Widget Flutter中使用换行符 [英] How to use new line character in Text Widget Flutter
本文介绍了如何在Text Widget Flutter中使用换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Flutter中显示多行文本?
How to display multiline text in Flutter?
Text("Text1\n Text2\n Text3",maxLines: 20, style: TextStyle(fontSize: 16.0 ,fontWeight:FontWeight.bold,color: Colors.black) , )
推荐答案
您可以使用三引号
参考链接 https://api.dartlang .org/stable/2.5.0/dart-core/String-class.html
方法1 使用三引号
child: Container(
child : Text('''
Text1
Text2
Text3''',maxLines: 20, style: TextStyle(fontSize: 16.0 ,fontWeight:FontWeight.bold,color: Colors.black) , )
),
方法2 使用此处的 \ n 是动态字符串的示例:
Approach 2 Using \n here is example with Dynamic String :
var readLines = ['Test1', 'Test2', 'Test3'];
String getNewLineString() {
StringBuffer sb = new StringBuffer();
for (String line in readLines) {
sb.write(line + "\n");
}
return sb.toString();
}
child: Container(
child: Text(
getNewLineString(),
maxLines: 20,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black),
)),
这篇关于如何在Text Widget Flutter中使用换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文