Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new line character in Text Widget Flutter

Tags:

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) , )
like image 983
Atmaram Avatar asked Sep 18 '19 12:09

Atmaram


People also ask

How do you add a new line in text widget in Flutter?

Approach 2 Using \n here is an 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.

How do I align text in next line in Flutter?

You can use the Row Widget to achieve the result you want: return new ListView(shrinkWrap: true, children: <Widget>[ new Row(children: <Widget>[ Text('1) '), Expanded(child: Text('I\'m dedicating every day to you')) ], crossAxisAlignment: CrossAxisAlignment.

How do you start a new line in darts?

Fortunately Dart has a built-in feature that allows us to split a String by newline. LineSplitter , which is part of Dart's convert API, can be used to split a String by CR , LF , or CR + LF . So, we don't need to create a new function. It returns List <String> , which means we can iterate the result.

What is a text widget in flutter?

A Text widget is simply a widget for displaying text on the screen: Text ("This is my text!"); The Text widget is a part of the flutter/material.dart package, and this package should be imported before using the widget in a flutter app.

What is a richtext widget?

See more widgets in the widget catalog. The text style to apply to descendant Text widgets without explicit style. The RichText widget displays text that uses multiple different styles. The text to display is described using a tree of TextSpan objects, each of which... A run of text with a single style. See more widgets in the widget catalog.

What is the default font for Android apps written in flutter?

The default font for an Android app written in Dart with the Flutter framework is Roboto. However, depending on the look-and-feel desired for the app, a special font may be required. Adding additional fonts, although relatively painless, involves adding or modifying files outside of the *.dart files you may be used to adding to or modifying.

What is flutter and why should you use it?

For the uninitiated, Flutter might appear like magic - lace up a couple of reactive views using functional code, update variables, set the state, and BAM! You have yourself an elegant app with beautiful views and animated interactions.


4 Answers

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

like image 191
Amit Prajapati Avatar answered Oct 17 '22 00:10

Amit Prajapati


For me, when getting data with '\n' in the text from the local sqlite database, this worked:

    var message = await db.getDataFromDB();
    return Text(message.replaceAll('\\n', '\n'))
like image 21
Amarok Avatar answered Oct 17 '22 00:10

Amarok


If you want to break line with a string that comes from outside the Flutter you should modify the string inside flutter.

So if you get from API a string 'Order received! \n Wait in the line!' and the break line is not working, in flutter you should replace the '\n' inside flutter

var response = await getMessageFromServer();
String message = getMessage(response);
return Text(message.replaceAll('\n', '\n'))

This way you will see how the color of '\n' is different in flutter.

I prefered using '/n' for the API and then in flutter I replace.all('/n', '\n')

like image 5
Gjomesquita Avatar answered Oct 17 '22 02:10

Gjomesquita


One other option I found is to use a RichText widget as follows:

RichText(
  text: TextSpan(
    text: 'A line with a newline character\n',
    children: [
      TextSpan(
        text: 'A second line',
      ),
    ],
  ),
),
like image 3
Eye Avatar answered Oct 17 '22 01:10

Eye