idkit_extension
一、 简介
idkit_extension 是 IDKit 系列 Flutter 包中的一员。它主要是对 Flutter 的一些类进行开发中常用功能的扩展。
二、扩展类
1. String
2. List
3. num
三、 扩展类功能
1. String
1. 去除字符串中所有的空格
2. 将字符串转化为数字,包括 Int 和 double。
3. 将字符串转化为集合
4. 判断字符串是否是合法的身份证
5. 判断字符串是否全部是汉字
6. 判断字符串是否是合法的手机号
7. 判断字符串是否全部是数字
8. 字符串金额格式化
9. 数字字符串保留指定位数的小数,并可控制是否四舍五入
2.List
1. 将集合元素转化为字符串
3.num
1. 将数字转化为指定小数位个数的字符串,并可控制是否四舍五入
四、 实例
/// 1. Remove all spaces in the string.
void removeAllSpaces() {
const String testStr = ' aa bb cc dd ';
print(testStr.trimAll());
}
/// 2. Convert a string to a number.
void toNumber() {
const String value = '110';
print(value.toInt()); // 110
print(value.toDouble()); // 110.0
}
/// 3. Convert string to collection.
void toCollection() {
const String testStr = 'abcd';
print(testStr.toList()); // [a,b,c,d]
const String testStr1 = '1,2,10,21';
print(testStr1.toList(patternSplit: ',')); // [1,2,10,21]
}
/// 4. ID card verification.
void toID() {
const String id1 = '53010219200508011X';
print(id1.isIDCard()); // true
const String id2 = '53010219200508012X';
print(id2.isIDCard()); // false
}
/// 5. Verify that the string is all Chinese characters.
void toCC() {
const String txt1 = '我是who';
print(txt1.isChineseCharacters()); // false
const String txt2 = '我是 大鲨鱼,是吗';
print(txt2.isChineseCharacters()); // true
}
/// 6. Verify that the string is a qualified phone number.
void toPhone() {
const String phone = '18801210281';
print(phone.isPhoneNumber()); // true
const String phone1 = '12801210281';
print(phone1.isPhoneNumber()); // false
}
/// 7. Verify whether it is a string composed of 0-9.
void toVerifyNumber() {
const String phone = '18801210281';
print(phone.isAllNumber()); // true
const String phone1 = '12801210281#';
print(phone1.isAllNumber()); // false
}
/// 8. Amount formatted in thousandths.
void toAmount() {
const String amount = '12345';
print(amount.thousands()); // 12,345
const String amount1 = '12345.087';
print(amount1.thousands()); // 12,345.087
}
/// 9. Splicing between collection elements.
void toStringFromList() {
const List<String> list = <String>['T', 'h', 'i', 's'];
print(list.splicing()); // This
const List<String> list1 = <String>['2021', '11', '17'];
print(list1.splicing('-')); // 2021-11-17
}
/// 11. Convert a string of numbers to a number with specified decimal places
void formStringToKeepPoint() {
final String a = '111.03'.keepFractionDigits(0);
print(a); // 111
final String b = '111.03'.keepFractionDigits(1);
print(b); // 111.0
final String c = '111.03'.keepFractionDigits(2);
print(c); // 111.03
final String d = '111.03'.keepFractionDigits(3);
print(d); // 111.030
final String e = '111'.keepFractionDigits(2);
print(e); // 111.00
final String f = '111.034'.keepFractionDigits(2);
print(f); // 111.03
final String f1 = '111.036'.keepFractionDigits(2);
print(f1); // 111.04
final String f2 = '111.036'.keepFractionDigits(2, round: false);
print(f2); // 111.03
final String g = '111.996'.keepFractionDigits(2);
print(g); // 112.00
final String g1 = '111.994'.keepFractionDigits(2);
print(g1); // 111.99
final String h = 'xx23343'.keepFractionDigits(3);
print(h); // 0.000
}
/// 12. Convert a string of numbers to a number with specified decimal places
void formNumToKeepPoint() {
final String a = 111.03.keepFractionDigits(0);
print(a); // 111
final String b = 111.03.keepFractionDigits(1);
print(b); // 111.0
final String c = 111.03.keepFractionDigits(2);
print(c); // 111.03
final String d = 111.03.keepFractionDigits(3);
print(d); // 111.030
final String e = 111.keepFractionDigits(2);
print(e); // 111.00
final String f = 111.034.keepFractionDigits(2);
print(f);// 111.03
final String f1 = 111.036.keepFractionDigits(2);
print(f1);// 111.04
final String f2 = 111.036.keepFractionDigits(2, round: false);
print(f2);// 111.03
final String g = 111.996.keepFractionDigits(2);
print(g);// 112.00
final String g1 = 111.994.keepFractionDigits(2);
print(g1); // 111.99
}