Skip to content

Commit 0c16553

Browse files
Merge pull request #75 from israelins85/main
Fixing the search of Country
2 parents b4a6728 + 1274170 commit 0c16553

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/src/form_builder_phone_field.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ class _FormBuilderPhoneFieldState
344344
if (phone.isNotEmpty) {
345345
try {
346346
final parseResult = PhoneNumber.parse(phone);
347+
final isoCode = parseResult.isoCode.name.split(".").last;
348+
Country country = CountryPickerUtils.getCountryByIsoCode(isoCode);
347349
setState(() {
348-
_selectedDialogCountry = CountryPickerUtils.getCountryByIsoCode(
349-
parseResult.countryCode,
350-
);
350+
_selectedDialogCountry = country;
351351
});
352352
_effectiveController.text = parseResult.nsn;
353353
} catch (error) {

test/form_builder_phone_field_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ void main() {
6060

6161
expect(formFieldKey.currentState?.value, '$validCodePhone$validPhone');
6262
});
63+
testWidgets('should found the correct country', (tester) async {
64+
final formFieldKey = GlobalKey<FormBuilderFieldState>();
65+
const fieldName = 'phone';
66+
const validPhone = '8599999999';
67+
const validCodePhone = '+55';
68+
final testWidget = FormBuilderPhoneField(
69+
name: fieldName,
70+
key: formFieldKey,
71+
);
72+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
73+
74+
formFieldKey.currentState?.didChange('$validCodePhone$validPhone');
75+
await tester.pumpAndSettle();
76+
77+
expect(formFieldKey.currentState?.value, '$validCodePhone$validPhone');
78+
});
79+
testWidgets('should not found the correct country', (tester) async {
80+
final formFieldKey = GlobalKey<FormBuilderFieldState>();
81+
const fieldName = 'phone';
82+
const validPhone = '8599999999';
83+
const invalidCodePhone = '+999';
84+
final testWidget = FormBuilderPhoneField(
85+
name: fieldName,
86+
key: formFieldKey,
87+
);
88+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
89+
90+
formFieldKey.currentState?.didChange('$invalidCodePhone$validPhone');
91+
await tester.pumpAndSettle();
92+
93+
expect(formFieldKey.currentState?.value, '$invalidCodePhone$validPhone');
94+
});
6395
});
6496
group('by FormKey -', () {
6597
testWidgets('should reset value when call reset', (tester) async {

0 commit comments

Comments
 (0)