add secure phone

null_safety
小赖 4 years ago
parent 576d826792
commit 272fc56de8

@ -20,6 +20,11 @@ class _ExampleStringExtState extends State<ExampleStringExt> {
title: Text('18888888888'), title: Text('18888888888'),
subtitle: Text('18888888888'.phone), subtitle: Text('18888888888'.phone),
), ),
ListTile(
leading: Chip(label: Text('.securePhone')),
title: Text('18888888888'),
subtitle: Text('18888888888'.securePhone),
),
], ],
), ),
); );

@ -1,10 +1,31 @@
extension PhoneExt on String { extension PhoneExt on String {
List get _parsePhone {
if (this.length < 11)
return [];
else
return [
this.substring(0, 3),
this.substring(3, 7),
this.substring(7, 11),
];
}
bool get _validPhone => _parsePhone.isNotEmpty;
///线
///
///example 18888888888 => 188-8888-8888
String get phone { String get phone {
///线 if (!_validPhone)
return (this.substring(0, 3) + return this;
'-' + else
this.substring(3, 7) + return '${_parsePhone[0]}-${_parsePhone[1]}-${_parsePhone[2]}';
'-' + }
this.substring(7, 11));
String get securePhone {
if (!_validPhone)
return this;
else
return '${_parsePhone[0]}****${_parsePhone[2]}';
} }
} }

Loading…
Cancel
Save