diff --git a/packages/firebase_ui_auth/lib/src/views/phone_input_view.dart b/packages/firebase_ui_auth/lib/src/views/phone_input_view.dart index 6d1ca2c4e598..16c4a59c38c5 100644 --- a/packages/firebase_ui_auth/lib/src/views/phone_input_view.dart +++ b/packages/firebase_ui_auth/lib/src/views/phone_input_view.dart @@ -90,7 +90,8 @@ class _PhoneInputViewState extends State { @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( flowKey: widget.flowKey, @@ -121,7 +122,7 @@ class _PhoneInputViewState extends State { widget.subtitleBuilder!(context), if (state is AwaitingPhoneNumber || state is SMSCodeRequested) ...[ PhoneInput( - initialCountryCode: countryCode!, + initialCountryCode: countryCode, onSubmit: onSubmit(ctrl), key: phoneInputKey, ), diff --git a/packages/firebase_ui_auth/lib/src/widgets/phone_input.dart b/packages/firebase_ui_auth/lib/src/widgets/phone_input.dart index d52bccaa7e28..ed2c5b08ba31 100644 --- a/packages/firebase_ui_auth/lib/src/widgets/phone_input.dart +++ b/packages/firebase_ui_auth/lib/src/widgets/phone_input.dart @@ -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 key) { @@ -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); diff --git a/packages/firebase_ui_auth/test/widgets/phone_input_test.dart b/packages/firebase_ui_auth/test/widgets/phone_input_test.dart new file mode 100644 index 000000000000..0002da215f35 --- /dev/null +++ b/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); + }, + ); + }); +}