Skip to content

Commit e87ec8a

Browse files
authored
fix(ui): allow null countryCode for phone input (#9937)
* fix(ui): allow null countryCode for phone input * get country code from platform dispatcher
1 parent 929e368 commit e87ec8a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Diff for: packages/firebase_ui_auth/lib/src/views/phone_input_view.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class _PhoneInputViewState extends State<PhoneInputView> {
9090
@override
9191
Widget build(BuildContext context) {
9292
final l = FirebaseUILocalizations.labelsOf(context);
93-
final countryCode = Localizations.localeOf(context).countryCode;
93+
final countryCode = Localizations.localeOf(context).countryCode ??
94+
WidgetsBinding.instance.platformDispatcher.locale.countryCode;
9495

9596
return AuthFlowBuilder<PhoneAuthController>(
9697
flowKey: widget.flowKey,
@@ -121,7 +122,7 @@ class _PhoneInputViewState extends State<PhoneInputView> {
121122
widget.subtitleBuilder!(context),
122123
if (state is AwaitingPhoneNumber || state is SMSCodeRequested) ...[
123124
PhoneInput(
124-
initialCountryCode: countryCode!,
125+
initialCountryCode: countryCode,
125126
onSubmit: onSubmit(ctrl),
126127
key: phoneInputKey,
127128
),

Diff for: packages/firebase_ui_auth/lib/src/widgets/phone_input.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class PhoneInput extends StatefulWidget {
8585

8686
/// An initial country code that should be selected in the country code
8787
/// picker.
88-
final String initialCountryCode;
88+
final String? initialCountryCode;
8989

9090
/// Returns a phone number from the [PhoneInput] that was provided a [key].
9191
static String? getPhoneNumber(GlobalKey<PhoneInputState> key) {
@@ -101,7 +101,7 @@ class PhoneInput extends StatefulWidget {
101101
/// {@macro ui.auth.widgets.phone_input}
102102
const PhoneInput({
103103
Key? key,
104-
required this.initialCountryCode,
104+
this.initialCountryCode,
105105
this.onSubmit,
106106
}) : super(key: key);
107107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:firebase_ui_auth/src/widgets/phone_input.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('PhoneInput', () {
7+
testWidgets('shows default country and country code', (tester) async {
8+
await tester.pumpWidget(
9+
const MaterialApp(
10+
home: Scaffold(
11+
body: PhoneInput(initialCountryCode: 'US'),
12+
),
13+
),
14+
);
15+
16+
expect(find.text('United States'), findsOneWidget);
17+
});
18+
19+
testWidgets(
20+
'prompts to select a country if initialCountryCode is null',
21+
(tester) async {
22+
await tester.pumpWidget(
23+
const MaterialApp(
24+
home: Scaffold(
25+
body: PhoneInput(initialCountryCode: null),
26+
),
27+
),
28+
);
29+
30+
expect(find.text('Choose a country'), findsOneWidget);
31+
},
32+
);
33+
});
34+
}

0 commit comments

Comments
 (0)