Skip to content

Commit de74051

Browse files
committed
initialCountryData instead of phone code
1 parent 951c6df commit de74051

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [2.11.0]
2+
- CountryDropdown now only selects initialCountryData instead of phone code
3+
because there are cases when different countries share the same phone code
4+
and we still need to tell them apart
15
## [2.10.9]
26
- Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/123
37
- Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/116

example/lib/pages/phone_format_page.dart

+8-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class _PhoneFormatPageState extends State<PhoneFormatPage> {
8787
flex: 3,
8888
child: CountryDropdown(
8989
printCountryName: true,
90-
initialPhoneCode: '7',
90+
initialCountryData:
91+
PhoneCodes.getPhoneCountryDataByCountryCode(
92+
'RU',
93+
),
9194
onCountrySelected: (PhoneCountryData countryData) {
9295
setState(() {
9396
_initialCountryData = countryData;
@@ -132,7 +135,10 @@ class _PhoneFormatPageState extends State<PhoneFormatPage> {
132135
flex: 3,
133136
child: CountryDropdown(
134137
printCountryName: true,
135-
initialPhoneCode: '7',
138+
initialCountryData:
139+
PhoneCodes.getPhoneCountryDataByCountryCode(
140+
'RU',
141+
),
136142
filter: PhoneCodes.findCountryDatasByCountryCodes(
137143
countryIsoCodes: [
138144
'RU',

example/pubspec.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ packages:
8989
path: ".."
9090
relative: true
9191
source: path
92-
version: "2.10.9"
92+
version: "2.11.0"
9393
flutter_test:
9494
dependency: "direct dev"
9595
description: flutter

lib/formatters/phone_input_formatter.dart

+16
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ class PhoneCountryData {
471471

472472
String? _maskWithoutCountryCode;
473473

474+
@override
475+
bool operator ==(covariant PhoneCountryData other) {
476+
return other.phoneCode == phoneCode &&
477+
other.internalPhoneCode == internalPhoneCode &&
478+
other.country == country;
479+
}
480+
481+
@override
482+
int get hashCode {
483+
return Object.hash(
484+
phoneCode,
485+
internalPhoneCode,
486+
country,
487+
);
488+
}
489+
474490
String getCorrectMask(String? countryCode) {
475491
if (countryCode == null) {
476492
return phoneMask!;

lib/widgets/country_dropdown.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CountryDropdown extends StatefulWidget {
99
final CountryItemBuilder? selectedItemBuilder;
1010
final CountryItemBuilder? listItemBuilder;
1111
final bool printCountryName;
12-
final String? initialPhoneCode;
12+
final PhoneCountryData? initialCountryData;
1313
final List<PhoneCountryData>? filter;
1414
final ValueChanged<PhoneCountryData> onCountrySelected;
1515

@@ -34,7 +34,7 @@ class CountryDropdown extends StatefulWidget {
3434

3535
/// [filter] if you need a predefined list of countries only,
3636
/// pass it here
37-
/// [initialPhoneCode] a phone code of the country without leading +
37+
/// [initialCountryData] initial country data to be selected
3838
/// [selectedItemBuilder] use this if you want to make
3939
/// the selected item look the way you want
4040
/// [listItemBuilder] the same as [selectedItemBuilder] but
@@ -50,7 +50,7 @@ class CountryDropdown extends StatefulWidget {
5050
this.selectedItemBuilder,
5151
this.listItemBuilder,
5252
this.printCountryName = false,
53-
this.initialPhoneCode,
53+
this.initialCountryData,
5454
this.triggerOnCountrySelectedInitially = true,
5555
this.filter,
5656
required this.onCountrySelected,
@@ -84,9 +84,9 @@ class _CountryDropdownState extends State<CountryDropdown> {
8484
@override
8585
void initState() {
8686
_countryItems = widget.filter ?? PhoneCodes.getAllCountryDatas();
87-
if (widget.initialPhoneCode != null) {
88-
_initialValue = _countryItems.firstWhereOrNull(
89-
(c) => c.phoneCode == widget.initialPhoneCode) ??
87+
if (widget.initialCountryData != null) {
88+
_initialValue = _countryItems
89+
.firstWhereOrNull((c) => c == widget.initialCountryData) ??
9090
_countryItems.first;
9191
}
9292
if (widget.triggerOnCountrySelectedInitially && _initialValue != null) {

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_multi_formatter
22
description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter
3-
version: 2.10.9
3+
version: 2.11.0
44
homepage: https://github.com/caseyryan/flutter_multi_formatter
55

66
environment:

0 commit comments

Comments
 (0)