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) , )
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.
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.
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.
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.
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.
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.
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.
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
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'))
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')
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',
),
],
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With