Skip to content

Commit

Permalink
fix(ui): allow null countryCode for phone input (#9937)
Browse files Browse the repository at this point in the history
* fix(ui): allow null countryCode for phone input

* get country code from platform dispatcher
  • Loading branch information
lesnitsky committed Nov 23, 2022
1 parent 929e368 commit e87ec8a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/firebase_ui_auth/lib/src/views/phone_input_view.dart
Expand Up @@ -90,7 +90,8 @@ class _PhoneInputViewState extends State<PhoneInputView> {
@override
Widget build(BuildContext context) {
final l = FirebaseUILocalizations.labelsOf(context);
final countryCode = Localizations.localeOf(context).countryCode;
final countryCode = Localizations.localeOf(context).countryCode ??
WidgetsBinding.instance.platformDispatcher.locale.countryCode;

return AuthFlowBuilder<PhoneAuthController>(
flowKey: widget.flowKey,
Expand Down Expand Up @@ -121,7 +122,7 @@ class _PhoneInputViewState extends State<PhoneInputView> {
widget.subtitleBuilder!(context),
if (state is AwaitingPhoneNumber || state is SMSCodeRequested) ...[
PhoneInput(
initialCountryCode: countryCode!,
initialCountryCode: countryCode,
onSubmit: onSubmit(ctrl),
key: phoneInputKey,
),
Expand Down
4 changes: 2 additions & 2 deletions packages/firebase_ui_auth/lib/src/widgets/phone_input.dart
Expand Up @@ -85,7 +85,7 @@ class PhoneInput extends StatefulWidget {

/// An initial country code that should be selected in the country code
/// picker.
final String initialCountryCode;
final String? initialCountryCode;

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

Expand Down
34 changes: 34 additions & 0 deletions packages/firebase_ui_auth/test/widgets/phone_input_test.dart
@@ -0,0 +1,34 @@
import 'package:firebase_ui_auth/src/widgets/phone_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('PhoneInput', () {
testWidgets('shows default country and country code', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: PhoneInput(initialCountryCode: 'US'),
),
),
);

expect(find.text('United States'), findsOneWidget);
});

testWidgets(
'prompts to select a country if initialCountryCode is null',
(tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: PhoneInput(initialCountryCode: null),
),
),
);

expect(find.text('Choose a country'), findsOneWidget);
},
);
});
}

0 comments on commit e87ec8a

Please sign in to comment.