From de7405158bf272ebde35f527aa39aee38c32775f Mon Sep 17 00:00:00 2001 From: Konstantin Serov Date: Wed, 10 May 2023 17:53:16 +0700 Subject: [PATCH] initialCountryData instead of phone code --- CHANGELOG.md | 4 ++++ example/lib/pages/phone_format_page.dart | 10 ++++++++-- example/pubspec.lock | 2 +- lib/formatters/phone_input_formatter.dart | 16 ++++++++++++++++ lib/widgets/country_dropdown.dart | 12 ++++++------ pubspec.yaml | 2 +- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf5741b..187f87e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2.11.0] +- CountryDropdown now only selects initialCountryData instead of phone code +because there are cases when different countries share the same phone code +and we still need to tell them apart ## [2.10.9] - Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/123 - Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/116 diff --git a/example/lib/pages/phone_format_page.dart b/example/lib/pages/phone_format_page.dart index e9503fa..5118a71 100644 --- a/example/lib/pages/phone_format_page.dart +++ b/example/lib/pages/phone_format_page.dart @@ -87,7 +87,10 @@ class _PhoneFormatPageState extends State { flex: 3, child: CountryDropdown( printCountryName: true, - initialPhoneCode: '7', + initialCountryData: + PhoneCodes.getPhoneCountryDataByCountryCode( + 'RU', + ), onCountrySelected: (PhoneCountryData countryData) { setState(() { _initialCountryData = countryData; @@ -132,7 +135,10 @@ class _PhoneFormatPageState extends State { flex: 3, child: CountryDropdown( printCountryName: true, - initialPhoneCode: '7', + initialCountryData: + PhoneCodes.getPhoneCountryDataByCountryCode( + 'RU', + ), filter: PhoneCodes.findCountryDatasByCountryCodes( countryIsoCodes: [ 'RU', diff --git a/example/pubspec.lock b/example/pubspec.lock index 37a752d..c70c56e 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -89,7 +89,7 @@ packages: path: ".." relative: true source: path - version: "2.10.9" + version: "2.11.0" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/formatters/phone_input_formatter.dart b/lib/formatters/phone_input_formatter.dart index ec2cd88..6a35b54 100644 --- a/lib/formatters/phone_input_formatter.dart +++ b/lib/formatters/phone_input_formatter.dart @@ -471,6 +471,22 @@ class PhoneCountryData { String? _maskWithoutCountryCode; + @override + bool operator ==(covariant PhoneCountryData other) { + return other.phoneCode == phoneCode && + other.internalPhoneCode == internalPhoneCode && + other.country == country; + } + + @override + int get hashCode { + return Object.hash( + phoneCode, + internalPhoneCode, + country, + ); + } + String getCorrectMask(String? countryCode) { if (countryCode == null) { return phoneMask!; diff --git a/lib/widgets/country_dropdown.dart b/lib/widgets/country_dropdown.dart index fa62ea6..218e04f 100644 --- a/lib/widgets/country_dropdown.dart +++ b/lib/widgets/country_dropdown.dart @@ -9,7 +9,7 @@ class CountryDropdown extends StatefulWidget { final CountryItemBuilder? selectedItemBuilder; final CountryItemBuilder? listItemBuilder; final bool printCountryName; - final String? initialPhoneCode; + final PhoneCountryData? initialCountryData; final List? filter; final ValueChanged onCountrySelected; @@ -34,7 +34,7 @@ class CountryDropdown extends StatefulWidget { /// [filter] if you need a predefined list of countries only, /// pass it here - /// [initialPhoneCode] a phone code of the country without leading + + /// [initialCountryData] initial country data to be selected /// [selectedItemBuilder] use this if you want to make /// the selected item look the way you want /// [listItemBuilder] the same as [selectedItemBuilder] but @@ -50,7 +50,7 @@ class CountryDropdown extends StatefulWidget { this.selectedItemBuilder, this.listItemBuilder, this.printCountryName = false, - this.initialPhoneCode, + this.initialCountryData, this.triggerOnCountrySelectedInitially = true, this.filter, required this.onCountrySelected, @@ -84,9 +84,9 @@ class _CountryDropdownState extends State { @override void initState() { _countryItems = widget.filter ?? PhoneCodes.getAllCountryDatas(); - if (widget.initialPhoneCode != null) { - _initialValue = _countryItems.firstWhereOrNull( - (c) => c.phoneCode == widget.initialPhoneCode) ?? + if (widget.initialCountryData != null) { + _initialValue = _countryItems + .firstWhereOrNull((c) => c == widget.initialCountryData) ?? _countryItems.first; } if (widget.triggerOnCountrySelectedInitially && _initialValue != null) { diff --git a/pubspec.yaml b/pubspec.yaml index 3272fb9..c7da2fd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_multi_formatter description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter -version: 2.10.9 +version: 2.11.0 homepage: https://github.com/caseyryan/flutter_multi_formatter environment: