From 272fc56de8041c8d9f02f75497ea0b534c526fce Mon Sep 17 00:00:00 2001 From: laiiihz Date: Thu, 17 Dec 2020 16:38:36 +0800 Subject: [PATCH] add secure phone --- .../data/extension/example_string_ext.dart | 5 +++ lib/extension/string_extension.dart | 33 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/example/lib/data/extension/example_string_ext.dart b/example/lib/data/extension/example_string_ext.dart index 175c227..9fff6ea 100644 --- a/example/lib/data/extension/example_string_ext.dart +++ b/example/lib/data/extension/example_string_ext.dart @@ -20,6 +20,11 @@ class _ExampleStringExtState extends State { title: Text('18888888888'), subtitle: Text('18888888888'.phone), ), + ListTile( + leading: Chip(label: Text('.securePhone')), + title: Text('18888888888'), + subtitle: Text('18888888888'.securePhone), + ), ], ), ); diff --git a/lib/extension/string_extension.dart b/lib/extension/string_extension.dart index 20ddbf7..37749d8 100644 --- a/lib/extension/string_extension.dart +++ b/lib/extension/string_extension.dart @@ -1,10 +1,31 @@ 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 { - ///电话号码加分隔线 - return (this.substring(0, 3) + - '-' + - this.substring(3, 7) + - '-' + - this.substring(7, 11)); + if (!_validPhone) + return this; + else + return '${_parsePhone[0]}-${_parsePhone[1]}-${_parsePhone[2]}'; + } + + String get securePhone { + if (!_validPhone) + return this; + else + return '${_parsePhone[0]}****${_parsePhone[2]}'; } }