flutter

How to use new line character in Text Widget 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) , )

Solution

  • Approach 1 Using Triple quotes

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

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

    Approach 3 using static text with \n

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

    For more, you should refer to this link https://api.dartlang.org/stable/2.5.0/dart-core/String-class.html