Skip to content

Commit

Permalink
initialCountryData instead of phone code
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed May 10, 2023
1 parent 951c6df commit de74051
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions example/lib/pages/phone_format_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ class _PhoneFormatPageState extends State<PhoneFormatPage> {
flex: 3,
child: CountryDropdown(
printCountryName: true,
initialPhoneCode: '7',
initialCountryData:
PhoneCodes.getPhoneCountryDataByCountryCode(
'RU',
),
onCountrySelected: (PhoneCountryData countryData) {
setState(() {
_initialCountryData = countryData;
Expand Down Expand Up @@ -132,7 +135,10 @@ class _PhoneFormatPageState extends State<PhoneFormatPage> {
flex: 3,
child: CountryDropdown(
printCountryName: true,
initialPhoneCode: '7',
initialCountryData:
PhoneCodes.getPhoneCountryDataByCountryCode(
'RU',
),
filter: PhoneCodes.findCountryDatasByCountryCodes(
countryIsoCodes: [
'RU',
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/formatters/phone_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
12 changes: 6 additions & 6 deletions lib/widgets/country_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhoneCountryData>? filter;
final ValueChanged<PhoneCountryData> onCountrySelected;

Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -84,9 +84,9 @@ class _CountryDropdownState extends State<CountryDropdown> {
@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) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit de74051

Please sign in to comment.