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 {
String get phone { 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;
///线 ///线
return (this.substring(0, 3) + ///
'-' + ///example 18888888888 => 188-8888-8888
this.substring(3, 7) + String get phone {
'-' + if (!_validPhone)
this.substring(7, 11)); return this;
else
return '${_parsePhone[0]}-${_parsePhone[1]}-${_parsePhone[2]}';
}
String get securePhone {
if (!_validPhone)
return this;
else
return '${_parsePhone[0]}****${_parsePhone[2]}';
} }
} }

Loading…
Cancel
Save