Non-breaking space in Flutter string interpolation - flutter

From time to time I need a non-breaking space in my Flutter Text widgets, e.g. a "Show more" link or a number with unit like "50 km/h".
The following code works fine but it looks overly complicated:
const int $nbsp = 0x00A0; // from https://pub.dev/packages/charcode
print('Hello${String.fromCharCode($nbsp)}World'); // --> prints "Hello World", does not break
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :/
I'm curious if there is a shorter way to use an integer constant from the charcode package in my interpolated string?

The easy way to do it is using escape combination [\u{00A0}]:
Text('Hello\u{00A0}world');

The best solution I have come up with is creating a String extension method.
// string_extension.dart
const int $nbsp = 0x00A0;
extension StringExtension on String {
String get nonBreaking => replaceAll(' ', String.fromCharCode($nbsp));
}
Usage example:
// import 'string_extension.dart';
Text('Hello World'.nonBreaking)

Related

Change number format thousands to K

Text(
'${NumberFormat.compactSimpleCurrency(
decimalDigits: 0).format(docSnap['count'])} searches',
Can you help me how to convert number for example 10,000 to 10K but without currency sign like $ or etc. it is because i just want to show only 10k instead of $10k
Install the intl package -->
https://pub.dev/packages/intl
Create custom function like this,
String formatNumber(int number) {
return NumberFormat.compact().format(number);
}
Call the formatNumber function when you want it.
How to use this with Text Widget,
int number = 100000; // Variable
Text(formatNumber(number),) // call the function inside Text widget

How can I get text value of TMPro Text without markup tags in Unity?

I am trying to get text value in TMPro Text component without markup tags but haven't found any solutions.
Say, <b><color=red>Hello </color><b> world is the value in TMPro Text, and I just want Hello world in c# script.
Bear in mind that tag <b> and color will change, so I would love to remove tags dynamically, meaning I would like not to replacing each tag by text.replace("<b>", "") kind of things.
Does anyone know how to do it?
I dont know about the option of using HTML on tmp but you can attach the text to your script by create a new variable like that:
[SerializeField] TMP_Text textVar;
then you can drag you tmp game object to the component that include this script
and the you can change the text like that:
textVar.text = "what ever";
or get text like that:
string textString = textVar.text;
for the color you can use
Color color = textVar.color;
You can use TMP_Text.GetParsedText () to get the text after it has been parsed and rich text tags removed.
Alternatively you can also use regular expressions to search a string for rich text tags, and remove them while preserving the rest of the string.
using System.Text.RegularExpressions;
public static string GetString (string str)
{
Regex rich = new Regex (#"<[^>]*>");
if (rich.IsMatch (str))
{
str = rich.Replace (str, string.Empty);
}
return str;
}

How can I detect a Paste event in a TextEditingController?

I have a TextEditingController which is for phone numbers. If text is pasted in it, I need to process and modify it, trimming whitespaces etc. But that should not happen if the user is typing in whitespaces. How can I differentiate between these two types of events, and call my function only when user pastes text into the field?
Currently my code looks like this and uses onChanged. I'm looking for something like onPaste:
String getCorrectedPhone(String phone) {
phone = phone.replaceAll(RegExp(r"\s+"), "");
return phone;
}
FormBuilderTextField(
controller: _phoneController,
name: "phone",
onChanged: (String txt) {
print('Phone field changed! Is now $txt');
_phoneController.text = getCorrectedPhone(txt);
},
),
You can do something like declare a length with the phone number field and add a listener to the text editing controller or in oNchanged which checks if its length - the old length is >1. Then its pasted
int length = 0;
...
_phoneController.addListener((){
if (abs(textEditingController.text.length - length)>1){
// Do your thingy
}
length = _phoneController.length;
});
So there is another way, that is to ignore any touches on the text field using the IgnorePointer widget and then use Gesture Detector to implement custom long tap and short taps. For long taps, you'll have to create your own small pop up menu for copy cut paste and stuff. Here is some sample code for the UI. As for the functioning, I would recommend using the https://api.flutter.dev/flutter/services/Clipboard-class.html class of flutter. If you need any help in doing this let me know but it should be mostly straightforward

Flutter/Dart programmatically unicode string

I have a list of customized icons to my app, it comes like below setted as IconData, note codePoint (0xe931).
IconData angry_face = IconData(0xe931, fontFamily: _fontFamily);
There's nothing wrong with that, but I've some cases where I need this icon as Unicode string to be used as a text. It should be done just like:
// It works too
Text('\ue931', style: TextStyle(fontFamily: _fontFamily));
The problem is:
I don't wanna use this code "by hand" because this icons are changed constantly by designers team and sometimes it changes its code messing up my app icons. What I need to do is get the icon object and parse it to the Unicode string, so I can use it with a Text widget.
I thought that would work to get programmatically that code and just use it, but it don't:
var iconcode = iconData.codePoint.toRadixString(16);
var result;
// Error: An escape sequence starting with '\u'
// must be followed by 4 hexadecimal digits or
// from 1 to 6 digits between '{' and '}'
result = '\u$iconcode';
// Just a simple string
result = '\\u$iconcode';
In few words: How can I parse programmatically int codePoint to a valid Unicode string?
Here's the right answer. I tried everything but this... Thank you #julemand101
final result = String.fromCharCode(iconData.codePoint);

Display country flag character in Flutter

This answer has some code to convert a locale to a country emoji in Java. I tried implementing it in Dart but no success.
I tried converting the code above to Dart
void _emoji() {
int flagOffset = 0x1F1E6;
int asciiOffset = 0x41;
String country = "US";
int firstChar = country.codeUnitAt(0) - asciiOffset + flagOffset;
int secondChar = country.codeUnitAt(1) - asciiOffset + flagOffset;
String emoji =
String.fromCharCode(firstChar) + String.fromCharCode(secondChar);
print(emoji);
}
"US" locale should output "🇺🇸"
The code you posted works correctly, i.e. print(emoji) successfully prints 🇺🇸.
I assume that the real problem you have is that the Flutter Text widget displays it like this:
It is the US flag, however, I have to agree that it does not look like it when you see it on device as the font size is very small and the flag has a rather high resolution.
You will need to use a custom font and apply it to your Text widget using the following:
Text(emoji,
style: TextStyle(
fontFamily: '...',
),
)
Otherwise, both the conversion and displaying the flags works fine. I believe that they just look different than you expected.