首页 > 解决方案 > 如何在 Text Widget Flutter 中使用换行符

问题描述

如何在 Flutter 中显示多行文本?

Text("Text1\n Text2\n Text3",maxLines: 20, style: TextStyle(fontSize: 16.0 ,fontWeight:FontWeight.bold,color: Colors.black) , )

标签: flutter

解决方案


方法 1使用三引号

      child: Container(
         child :  Text('''
                          Text1
                          Text2
                          Text3''',maxLines: 20, style: TextStyle(fontSize: 16.0 ,fontWeight:FontWeight.bold,color: Colors.black) , )
      ),

方法 2使用\n这里是动态字符串的示例:

        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),
        )),

方法 3使用带有 \n 的静态文本

  Text('Welcome\nto\nMyWorld\nHello\nWorld\n');

有关更多信息,您应该参考此链接 https://api.dartlang.org/stable/2.5.0/dart-core/String-class.html


推荐阅读