Formatting text in flutter - flutter

I am styling text but am not able to get what I want like -:
I want this text below
look like this -:
how do I do it can anyone help?

If you want each word on a new line
String text = "Lorem ipsum dolor sit amet";
Text(
text.replaceAll(" ", "\n"),
),
text.replaceAll(" ", "\n") will replace all space with \n(new line)

You can use \n (optn + shift + 7) to make a new line inside you're String.
Container(
child: Center(
child: Text('Hello \nWorld',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.bold,
letterSpacing: 0.7
)
),
),
),
Use textAlign to make your text align in the center (default is align left).

Its seem like by your comment you have a dynamic data. Since your question is not very clear, I will give you sample example to handle dynamic Data.
List a = ['Hello World', 'Hello', 'World'];
ListView(
children: a.map((e) {
if (e == "Hello") {
return Text(
e,
textAlign: TextAlign.end,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
} else if (e == "World") {
return Text(
e,
textAlign: TextAlign.end,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
} else {
return Text(
e,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
);
}
}).toList()),
Code Explanation
1). I have declare a Random List of String.
2). Then I used List View to handle dynamic list.
3). I put iteration and use a Text widget and put some style according to my Requirment.
you can put condition by yourself what you needed.

You can use \n to make a new line
Code:
Center(
child: Text('Hello Guys, Thanks \nfor visiting our website \nFlutter Examples.com',
style: TextStyle(fontSize: 22),
textAlign: TextAlign.center)
)

Related

Dynamically load list of words from API as map in highlight_text: ^1.6.0

I need to highlight some words in a text. I used highlight_text 1.6.0.
Map<String, HighlightedWord> words = {
"Flutter": HighlightedWord(
textStyle: textStyle,
),
"words": HighlightedWord(
textStyle: textStyle,
),
};
TextHighlight(
text: text,
words: words,
matchCase: false,
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.black,
),
textAlign: TextAlign.justify,
)
It is working fine when given static values.
Is it possible to load the highlighted words from API like a list instead of the map?
List=["Flutter", "Words"]
Lets assume you get this sample list from api:
List sample = ["Flutter", "Words"];
then you can use fromIterable:
var result = Map<String, HighlightedWord>.fromIterable(sample,
key: (v) => v,
value: (v) => HighlightedWord(
textStyle: textStyle,
),
);
then use result in TextHighlight:
TextHighlight(
text: text,
words: result,
matchCase: false,
textStyle: TextStyle(
fontSize: 14.0,
color: Colors.black,
),
textAlign: TextAlign.justify,
)

How to diplay a hexadecimal emoji in flutter

I would like to display the emoji 🤑 while only having the hexadecimal value of it.
var line = "testing is forever &#x1f911;";
Text(
"${line}",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Color(0xFF008080)),
),
Try below code hope its helpful to you. Refer Unicode Charecter here
Text(
'\u{1F602}',
style: TextStyle(
fontSize: 25,
),
),
Your Result screen->
Or in VS Code Pressed Window + . and open the emoji picker box choose your wanted emoji and select it
Text(
'🤑',
style: TextStyle(
fontSize: 30,
),
),
Your result screen->
I was able to fix this by using the HtmlCharacterEntities and the HtmlUnescape
HtmlUnescape().convert(HtmlCharacterEntities.decode("testing is forever 🤑"))

How to capitalize text in Flutter

I tried to find how capitalize text in Flutter, but I couldn't find it.
My code:
Center(
heightFactor: 2,
child: Text(
'Strengthening the bond of owners and pets, more than ever...',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),
I dont know if there is a way to do it through the Text widget, but you can use string.toUppercase() to capitalize the word:
Center(
heightFactor: 2,
child: Text(
'Strengthening the bond of owners and pets, more than ever...'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),
To capitalize and to uppercase are different:
Capitalize: "Strengthing..."
Uppercase: "STRENGTHING..."
Capitalize
To capitalize a string in all locales:
import 'package:intl/intl.dart';
toBeginningOfSentenceCase('strengthening...');
To capitalize a string for en-US like locales:
String text = 'strengthening...';
text = text[0].toUpperCase() + text.substring(1).toLowerCase();
You can also have the virtual keyboard automatically switch to uppercase on the start of a sentence:
TextField(
textCapitalization: TextCapitalization.sentences
)
https://api.flutter.dev/flutter/services/TextCapitalization.html
Uppercase
To uppercase a string:
'strengthening...'.toUpperCase()
You can also have the virtual keyboard automatically switch to uppercase on each character
TextField(
textCapitalization: TextCapitalization.characters
)
https://api.flutter.dev/flutter/services/TextCapitalization.html
Better way to capitalize text
// copy this code to somewhere in your dart file
// also you can write/add methods to below code to support your strings to modify i.e. `toCapitalize()` or `yourStringModifyingMethod`.
extension StringCasingExtension on String {
String toCapitalize() => length > 0 ? ${this[0].toUpperCase()}${substring(1).toLowerCase()}' : '';
// String yourStringModifyingMethod() => write your logic here to modify the string as per your need;
}
Here is how you can use it
var word = 'this is capital'.toCapitalized(); // 'This is capital'
Well, you could use this package to capitalize the content in the Text widget:
https://pub.dev/packages/text_tools.
//This will put the letter in position 15 in UpperCase, will print 'Strengthening The bond of owners and pets, more than ever...'
Center(
heightFactor: 2,
child: Text(
TextTools.toUppercaseAnyLetter(text: 'Strengthening the bond of owners and pets, more than ever...', position: 15),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
color: Colors.cyanAccent[700],
wordSpacing: 8,
),
)),

Update flutter font with a function

I want to update the displaying font in flutter using a function.I've tried with following method but it won't update.I can't find the problem.
Function
_fontselect(String ){
if (_character==1) {
return "Font";
} else{
return "Font2";
}
}
context
Center(child: Text(text,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily:_fontselect(String),
fontWeight: FontWeight.bold,
fontSize: 28.0,
color: Colors.red)
),
),
Use Ternary Operator within the fontFamily itself.
Center(child: Text(text,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: _character==1 ? "Font" : "Font2",
fontWeight: FontWeight.bold,
fontSize: 28.0,
color: Colors.red)
),
),
Your function needs a String variable to use inside of the function.
_fontselect(String character){
if (character==1) {
return "Font";
} else{
return "Font2";
}
}
Also it looks like character is an int but you are sending a String over to the function you should call the function with an int if this makes more sense.

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) , )
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',
),
],
),
),
Just use this
Text('${userInfo.name}'
'\n${userInfo.email}'
'\n${userInfo.mobilenumber}',maxLines: 20, style: TextStyle(fontSize: 16.0 ,fontWeight:FontWeight.bold,color: Colors.black) , )
You can write multiple lines of text in the Text() widget by simply enclose every line of text with a separate quote(" ").
Like this, look at the screenshot.