diff --git a/packages/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart b/packages/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart index 3935acc4cc..b72cfff9a5 100644 --- a/packages/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart +++ b/packages/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart @@ -16,6 +16,7 @@ import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; import 'package:amplify_authenticator/src/l10n/auth_strings_resolver.dart'; import 'package:amplify_authenticator/src/models/username_input.dart'; +import 'package:amplify_authenticator/src/state/authenticator_state.dart'; import 'package:amplify_authenticator/src/utils/validators.dart'; import 'package:amplify_authenticator/src/widgets/component.dart'; import 'package:amplify_authenticator/src/widgets/form.dart'; @@ -129,19 +130,29 @@ mixin AuthenticatorUsernameField return {...?authConfig?.usernameAttributes}; }(); - /// Toggle value for the email or phone number case. - final ValueNotifier useEmail = ValueNotifier(true); - UsernameConfigType get usernameType { if (usernameAttributes.isEmpty) { return UsernameConfigType.username; @@ -283,7 +291,7 @@ mixin UsernameAttributes case UsernameConfigType.phoneNumber: return UsernameType.phoneNumber; case UsernameConfigType.emailOrPhoneNumber: - if (useEmail.value) { + if (state.usernameSelection == UsernameSelection.email) { return UsernameType.email; } return UsernameType.phoneNumber; diff --git a/packages/amplify_authenticator/lib/src/state/authenticator_state.dart b/packages/amplify_authenticator/lib/src/state/authenticator_state.dart index 3954b8c580..f941bc1eee 100644 --- a/packages/amplify_authenticator/lib/src/state/authenticator_state.dart +++ b/packages/amplify_authenticator/lib/src/state/authenticator_state.dart @@ -21,6 +21,12 @@ import 'package:amplify_authenticator/src/state/auth_state.dart'; import 'package:amplify_authenticator/src/utils/country_code.dart'; import 'package:flutter/material.dart'; +/// {@macro amplify_authenticator.authenticator_state.username_selection} +enum UsernameSelection { + email, + phoneNumber, +} + @visibleForTesting typedef BlocEventPredicate = bool Function(AuthState state); @@ -98,6 +104,21 @@ class AuthenticatorState extends ChangeNotifier { String _username = ''; + /// {@template amplify_authenticator.authenticator_state.username_selection} + /// The username type to use during sign up and sign in. + /// + /// For auth configurations that allow sign up via username or email, + /// this will determine which option is currently selected. + /// {@endtemplate} + UsernameSelection get usernameSelection => _usernameSelection; + + set usernameSelection(UsernameSelection value) { + _usernameSelection = value; + notifyListeners(); + } + + UsernameSelection _usernameSelection = UsernameSelection.email; + /// The value for the password form field /// /// This value will be used during sign up, sign in, or other actions diff --git a/packages/amplify_authenticator/lib/src/widgets/form.dart b/packages/amplify_authenticator/lib/src/widgets/form.dart index 6ecd48482e..48381c52a1 100644 --- a/packages/amplify_authenticator/lib/src/widgets/form.dart +++ b/packages/amplify_authenticator/lib/src/widgets/form.dart @@ -142,26 +142,6 @@ class AuthenticatorFormState final ValueNotifier obscureTextToggleValue = ValueNotifier(true); - @override - void initState() { - super.initState(); - useEmail.addListener(_updateUseEmail); - } - - @override - void dispose() { - useEmail.removeListener(_updateUseEmail); - super.dispose(); - } - - void _updateUseEmail() { - // Clear attributes on switch - state.authAttributes.clear(); - - // Refresh state - setState(() {}); - } - /// Controls optional visibility of the field. Widget get obscureTextToggle { return ValueListenableBuilder( diff --git a/packages/amplify_authenticator/lib/src/widgets/form_field.dart b/packages/amplify_authenticator/lib/src/widgets/form_field.dart index dc7057d7c0..df0eaf0089 100644 --- a/packages/amplify_authenticator/lib/src/widgets/form_field.dart +++ b/packages/amplify_authenticator/lib/src/widgets/form_field.dart @@ -140,10 +140,6 @@ abstract class AuthenticatorFormFieldState AuthenticatorFormState.of(context).selectedUsernameType; - @nonVirtual - ValueNotifier get useEmail => - AuthenticatorFormState.of(context).useEmail; - /// Callback for when `onChanged` is triggered on the [FormField]. ValueChanged get onChanged => (_) {}; @@ -255,8 +251,6 @@ abstract class AuthenticatorFormFieldState('usernameType', usernameType)); properties.add(EnumProperty( 'selectedUsernameType', selectedUsernameType)); - properties - .add(DiagnosticsProperty>('useEmail', useEmail)); properties.add(IntProperty('maxLength', maxLength)); properties.add(DiagnosticsProperty('isOptional', isOptional)); properties.add(StringProperty('labelText', labelText));