Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): allow null countryCode for phone input #9937

Merged
merged 2 commits into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be most of the time null, because translations will mostly be on language code and not include a countryCode. It would be helpful to check WidgetsBinding.instance.platformDispatcher.locale.countryCode in case Localizations.localeOf(context).countryCode (L93) is null.

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);
},
);
});
}