Filled Stacks
Filled Stacks

Reputation: 4346

Text read from disk does not comply with new line character

Using the Text widget with text that's read from disk, that contains new line characters \n, does not go to new line. Anyone have suggestions of how I can get around this? The text in the file is enter image description here

The code for the image you see above is as follows:

Text(valueFromFile, style: TextStyle(color: Colors.red))

My text is being read from an Xml file that is contained in my assets folder.

Upvotes: 4

Views: 992

Answers (2)

Filled Stacks
Filled Stacks

Reputation: 4346

The comment from uaraven above

Does your file contain actual '\n' characters? If so then that's not a new line character ...

Helped me come up with a solution. Since the Text Widget is interpreting the \n characters as normal characters, I just did a replace on it and inserted the actual escape character.

var correctlyEscapedString = valueFromFile.replaceAll('\\n', '\n');
Text(correctlyEscapedString, style: TextStyle(color: Colors.red));

Upvotes: 5

Scaphae Studio
Scaphae Studio

Reputation: 551

final String someText = 
"This is your line number one\n\n"
"This is your line number two\n\n"
"This is your line number three\n\n"
"This is your line number four\n\n"
"This is your line number five\n\n";

this is one option for you.

int maxLines
final

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow.

If this is 1, text will not wrap. Otherwise, text will be wrapped at the edge of the box.

If this is non-null, it will override even explicit null values of Text.maxLines.

Upvotes: 0

Related Questions