Change number format thousands to K - flutter

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

Related

How can I display the number 10525.25 as 10.525,25 using regexp with Dart?

I want to change my number format. If number like 10525.25 I want to make like this = 10.525,25 If number is 40000.25 it must be like 4.000,25 How can I make this in dart? I use Flutter and I will show this in my Text widget.
The intl package is what you are looking for. It will format numbers in any locale for you. The format you use in your examples is the same as German, for example:
import 'package:intl/intl.dart';
void main () {
final fmt = NumberFormat("#,##0.00", "de_DE");
print(fmt.format(10525.25));
print(fmt.format(40000.25));
}
Output:
10.525,25
40.000,25
You may need to tweak the first parameter to NumberFormat to suit your formatting requirement.

How to put variable text and custom text together in flutter

If there is an inherited variable called id, I would like to display widget.index and ": problem number" together on one line on the screen.
ex)
problem(text) 1(list variable)
problem(text) 2(list variable)
Is there a way to get a variable in a list like the one above and display it on the screen along with text?
You can use $ in a string
String someString = 'problem id: ${problem(text)} and widget index :${widget.index}';

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

Text style in google script TextItem title

I have written an app to automatically update an order form everytime an order is passed. Currently, the form consists in N Textitems, which titles are like:
Product (remains : [number of remaining products])
Product description
This is performed by the following lines :
var Products= wsStocks.getRange(1,1,wsStocks.getLastRow(),1).getValues();
var Description = wsStocks.getRange(1,2,wsStocks.getLastRow(),2).getValues();
var Qtys = wsStocks.getRange(1,3,wsStocks.getLastRow(),3).getValues();
for(j=0;j<Products.length;j++){
Items[j].setTitle( `${Products[j][0]} (remains: ${Qtys[j][0]})`+ "\n" +`${Description[j][0]}`;
};
I would like to set a text style for this title : I want the information on the number of remaining products to be in italic, and the description to be in small size. But while I have found how to set the style for a Text variable, I can't find how to do this for a string used in the setTitle method ?
You should get the Range of each item from the Items array first and then you should be able to change the TextStyle according to your needs by using the setTextStyle() method.
For customizing the text styles, you can create your own, something similar to this.
// italic text style
var style1 = SpreadsheetApp.newTextStyle()
.setFontSize(14)
.setItalic(true)
.build();
// small size text style
var style2 = var style = SpreadsheetApp.newTextStyle();
.setFontSize(11)
.build();
Afterwards, for each element of the Items array, you should do this
for(j = 0; j < Products.length; j++)
sheet.getRange("RANGE_OF_Items[j]_ELEMENT").setTextStyle(style1); //or style2
Reference
Apps Script Class TextStyle;
Apps Script Range Class - setTextStyle(style).

Non-breaking space in Flutter string interpolation

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)