How to show emoji in flutter - flutter

I have an api from which I am fetching messages. These messages might contain emoji.
When it does contain emoji Text Widget is not displaying it correctly.
I just want to display them on the screen. Help if possible.
Thank you :)
class Message{
String text;
Message(){
this.text = get_data_from_api()['text'];
}
}
message = Message();
return Container(
child: Text(message.text),
);
No Displaying text with Emojis on Flutter
won't do it. I can do that but it only works if you have emoji in a string, that is explicitly plasing an emoji in string works fine for me. but when I am using something similar to the code above. I am getting, this
enter image description here
digging more into it I found that if I print my response on console it is
"ð­ð­ð­ð­"
and if I print a emoji it is 😭
So
I am using
Response.fromStream(await request.send())
from http/http.dart
So is that the problem ?

I did figured it out
I was taking data from an API. So while decoding for some reason it was not decoding utf-8 so what I needed to do was add
utf8.decode(response.bodyBytes);
in place of response.body and it was solved
Reference: Emoji and accent encoding in dart/flutter

If you have an emoji image explicitly present in the response from your API and you want to display it in your text, your should try adding WidgetSpan inside your RichText below is an example of how to do it :
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
),
In the above example they have used the material icon( Icon(Icons.add)). If you want to replace that from the image your get from the API, you can use ImageIcon instead of Icon widget. Follow this link to know how to use ImageIcon : https://www.woolha.com/tutorials/flutter-using-imageicon-widget-examples
Reference : Add image to RichText element

Related

Text widget cannot read html tags Flutter

Text widget in Flutter dose not support a special charterers like &, where sometimes this charterer comes like "first & second" from the database, or "&quot" should be ".
Text widget should read/convert these characters automatically. I have already fixed by following lines, But I need to put this solution in all Text widgets I have in my app.
Is it possible to give all Text widgets the possibility to read these special charterers directly in main function in runAPP or "MaterialApp" without putting the following lines in all Text widgets the app has?
final document = parse(htmlString);
final String parsedString = parse(document.body.text).documentElement.text;
Use this package flutter_html and try as follows:
Html(data: htmlString, style: {
"body": Style(
fontSize: FontSize(18),
color: Colors.black,
fontWeight: FontWeight.normal)
}),

How to create `IconData` from text in flutter

I want to create a icon from a text like currency symbol. Currency symbols are not available in material icons and i don't want to use any third party library. I want to convert the text string like '$' to Icons and use them.
As per your requirement try below code hope its help to you:
Using \ character
Text(
'\$ Search',
),
Your Output Screen like->
Using Flutter Unicode character. You found Unicode character here
Text(
'Your \u{1F4B2} Symbol',
),
Your result screen like->
You can add any Symbol in between text
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text('Your'),
Icon(Icons.add),
Text('Icon'),
],
),
Your result screen like->
Or in VS Code you can used Windows + . keys and add any symbol on your need
Please try this one. 0024 is Unicode for $
Icon( IconData(0x0024, fontFamily: 'MaterialIcons'),size: 50, ))

Flutter: How to use Material Icons in String?

I have a string and I would like to have an icon from the material icons displayed in that string. Currently, the only workaround is to use the standard emoji codes (e.g. '\u26A0').
I'm not quite sure how to do this. The reason I can't use a widget here is because I'm using a validator, and if the amount of characters are not met, the validator returns a string that reads out what the issue is. I'm trying to add a warning icon to it.
Would appreciate some help, thanks in advance!
The solution to your use case is WidgetSpan here is simple example of how to use it
Text.rich(
TextSpan(
children: <InlineSpan>[
TextSpan(text: 'call me'),
WidgetSpan(
child:Icon(Icons.phone)
),
],
)
),

Flutter RichText or other markup of dynamic content

I'm trying to display formatted text that is created dynamically in the app. It is the result of a diff calculation and I can't see how RichText or any of the markdown formats would work. It doesn't seem like embedding HTML is the way to go, given that it seems that would display in a frame.
The code shows a static presentation of the code. I need to be able to use a list of objects and make an corresponding array of TextSpans. I'm not sure there is a way to do this.
I should add that the Styles I want are what you would see if editing text in MS Word: additions are colored green and deletions are red and crossed through. This part is easy in with RichText styles, but aren't supported by Markdown.
//This list of objects has the data I use will use to create the TextSpans.
List<DiffString> diffOut = diff(
oldStr,
newStr,
" ",
).pieces;
//
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'Hello ',
style: normalStyle,
),
TextSpan(
text: 'added',
style: addedStyle,
),
TextSpan(
text: 'deleted',
style: deletedStyle,
),
TextSpan(
text: ' \n\n',
style: normalStyle,
),
//TextSpan(text: 'default is messed up'),
],
),
),
pskink answered my question but then deleted it, so I'm posting it here. And yes, it just needed to map it like he said. Thanks!
/////////Here:
all you need is a List of TextSpans created by your diff calculation method and pass it as children: myListOfTextSpanDiffs - so for example: var myListOfTextSpanDiffs = diffOut.map(...).toList()

Is there an easy way to find particular text built from RichText in a Flutter test?

For example, I may have one RichText in current widget tree, that looks like
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
)
I try to use find.text('Hello bold world!') but it doesn't work because it's not a Text.
Framework solution
I have recently contributed this feature to the Flutter framework, i.e. to the built-in finders.
find.text()
You can now enable a findRichText parameter, which will then also find standalone RichText widgets:
find.text(
'Hello bold world!',
findRichText: true,
)
Simplest solution is to put a key on the RichText and read it that way.
If that's not a good fit for whatever reason, you can use find.byWidgetPredicate and pass a function that matches RichText widgets whose text.toPlainText() returns the string you want.
Here's the find.byWidgetPredicate call.
find.byWidgetPredicate((widget) => fromRichTextToPlainText(widget) == 'Hello bold world!')
Here's the fromRichTextToPlainText helper function. Pass it the RichText widget, it will return the plain text.
String fromRichTextToPlainText(final Widget widget) {
if (widget is RichText) {
if (widget.text is TextSpan) {
final buffer = StringBuffer();
(widget.text as TextSpan).computeToPlainText(buffer);
return buffer.toString();
}
}
return null;
}
I solved for this by digging into the widget a bit more manually
final richTextWidget = tester.element(richTextFinder).widget as RichText;
print(richTextWidget.text.children);
With the children, I can assert they are generated as expected