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

Parvatijay2901/update signup_and_login_botton_color #130

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
496dd11
Updated SignUp Button to remain grey till all the entries are valid
Oct 17, 2024
7f32201
Updated Login Button to remain grey till all the entries are valid
Oct 17, 2024
e72e451
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 17, 2024
f891c1a
test: setup CI and framework for unit tests (#129)
anujsinha3 Oct 18, 2024
def308d
Move isAllFieldsFilled logic to LoginCubit
Oct 22, 2024
2ef61b9
Move isAllFieldsFilled logic to SignupCubit
Oct 22, 2024
32e69b0
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 22, 2024
7b22594
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 23, 2024
b5f9a5d
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 23, 2024
f437df3
Update the button to activate only when it is valid and have all the …
Oct 24, 2024
8c7038d
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 24, 2024
2182946
refactor: Make hardcoded string for routes and app roles to constants…
lsetiawan Oct 24, 2024
768f273
feat: Add data analysis framework (#153)
lsetiawan Oct 24, 2024
7537af8
Fix isValid bug
Oct 25, 2024
bc2a9aa
Split field and status listeners in Signup/Login forms
Oct 25, 2024
498d4f9
Merge branch 'main' into parvatijay2901/update-signupbotton
parvatijay2901 Oct 25, 2024
7c81460
Build both password/confirm password again when either of them change
Oct 25, 2024
87a334b
Show Validation Error while filling out the fields
Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/support_sphere/lib/logic/cubit/login_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@ class LoginCubit extends Cubit<LoginState> implements ValidatableCubit {
final AuthenticationRepository _authenticationRepository;

void emailChanged(String value) {
emit(
state.copyWith(
email: value,
),
);
emit(state.copyWith(email: value));
}

void passwordChanged(String value) {
emit(
state.copyWith(
password: value,
),
);
emit(state.copyWith(password: value));
}

void toggleShowPassword() => changeShowPassword(emit, state);

void setValid() => emit(state.copyWith(isValid: true));
void setInvalid() => emit(state.copyWith(isValid: false));

void toggleShowPassword() {
changeShowPassword(emit, state);
bool isLoginButtonEnabled() {
return state.isValid && state.isAllFieldsFilled;
}

void validateAllFieldsFilled() {
bool isAllFieldsFilled = state.email.isNotEmpty && state.password.isNotEmpty;
emit(state.copyWith(isAllFieldsFilled: isAllFieldsFilled));
}

Future<void> logInWithCredentials() async {
if (!state.isValid) return;
if (!state.isValid && !state.isAllFieldsFilled) return;
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
try {
await _authenticationRepository.logIn(
Expand Down
6 changes: 5 additions & 1 deletion src/support_sphere/lib/logic/cubit/login_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class LoginState extends Equatable {
this.password = '',
this.status = FormzSubmissionStatus.initial,
this.isValid = false,
this.isAllFieldsFilled = false,
this.showPassword = false,
this.errorMessage,
});
Expand All @@ -14,17 +15,19 @@ final class LoginState extends Equatable {
final String password;
final FormzSubmissionStatus status;
final bool isValid;
final bool isAllFieldsFilled;
final bool showPassword;
final String? errorMessage;

@override
List<Object?> get props => [email, password, status, isValid, errorMessage, showPassword];
List<Object?> get props => [email, password, status, isValid, isAllFieldsFilled, errorMessage, showPassword];

LoginState copyWith({
String? email,
String? password,
FormzSubmissionStatus? status,
bool? isValid,
bool? isAllFieldsFilled,
bool? showPassword,
String? errorMessage,
}) {
Expand All @@ -33,6 +36,7 @@ final class LoginState extends Equatable {
password: password ?? this.password,
status: status ?? this.status,
isValid: isValid ?? this.isValid,
isAllFieldsFilled: isAllFieldsFilled ?? this.isAllFieldsFilled,
errorMessage: errorMessage ?? this.errorMessage,
showPassword: showPassword ?? this.showPassword,
);
Expand Down
53 changes: 22 additions & 31 deletions src/support_sphere/lib/logic/cubit/signup_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,52 @@ class SignupCubit extends Cubit<SignupState> implements ValidatableCubit {
final UserRepository _userRepository;

void firstNameChanged(String value) {
emit(
state.copyWith(
givenName: value,
),
);
emit(state.copyWith(givenName: value));
}

void lastNameChanged(String value) {
emit(
state.copyWith(
familyName: value,
),
);
emit(state.copyWith(familyName: value));
}

void emailChanged(String value) {
emit(
state.copyWith(
email: value,
),
);
emit(state.copyWith(email: value));
}

void passwordChanged(String value) {
emit(
state.copyWith(
password: value,
),
);
emit(state.copyWith(password: value));
}

void signupCodeChanged(String value) {
emit(
state.copyWith(
signupCode: value,
),
);
emit(state.copyWith(signupCode: value));
}

void confirmedPasswordChanged(String value) {
emit(
state.copyWith(
confirmedPassword: value,
),
);
emit(state.copyWith(confirmedPassword: value));
}

void toggleShowPassword() => changeShowPassword(emit, state);

void setValid() => emit(state.copyWith(isValid: true));
void setInvalid() => emit(state.copyWith(isValid: false));

bool isSignupButtonEnabled() {
return state.isValid && state.isAllFieldsFilled;
}

void validateAllFieldsFilled() {
bool isAllFieldsFilled = state.givenName.isNotEmpty &&
state.familyName.isNotEmpty &&
state.email.isNotEmpty &&
state.password.isNotEmpty &&
state.confirmedPassword.isNotEmpty &&
state.signupCode.isNotEmpty;
emit(state.copyWith(isAllFieldsFilled: isAllFieldsFilled));
}

/// Sign up with email and password.
Future<void> signUpWithEmailAndPassword() async {
// If the form is invalid, do nothing
if (!state.isValid) return;
if (!state.isValid && !state.isAllFieldsFilled) return;
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
try {
// TODO: Add coupon code check for signup
Expand Down
5 changes: 5 additions & 0 deletions src/support_sphere/lib/logic/cubit/signup_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SignupState extends Equatable {
this.status = FormzSubmissionStatus.initial,
this.isValid = false,
this.showPassword = false,
this.isAllFieldsFilled = false,
this.errorMessage,
});

Expand All @@ -23,6 +24,7 @@ class SignupState extends Equatable {
final FormzSubmissionStatus status;
final bool isValid;
final bool showPassword;
final bool isAllFieldsFilled;
final String? errorMessage;

@override
Expand All @@ -35,6 +37,7 @@ class SignupState extends Equatable {
signupCode,
status,
isValid,
isAllFieldsFilled,
errorMessage,
showPassword,
];
Expand All @@ -48,6 +51,7 @@ class SignupState extends Equatable {
String? signupCode,
FormzSubmissionStatus? status,
bool? isValid,
bool? isAllFieldsFilled,
bool? showPassword,
String? errorMessage,
}) {
Expand All @@ -60,6 +64,7 @@ class SignupState extends Equatable {
signupCode: signupCode ?? this.signupCode,
status: status ?? this.status,
isValid: isValid ?? this.isValid,
isAllFieldsFilled: isAllFieldsFilled ?? this.isAllFieldsFilled,
errorMessage: errorMessage ?? this.errorMessage,
showPassword: showPassword ?? this.showPassword,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,32 @@ class LoginForm extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocListener<LoginCubit, LoginState>(
listenWhen: (previous, current) => previous.status != current.status,
listener: (context, state) {
if (state.status.isFailure) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
const SnackBar(
content: Text('Authentication Failure'),
),
);
}
},
return MultiBlocListener(
listeners: [
BlocListener<LoginCubit, LoginState>(
listenWhen: (previous, current) =>
previous.email != current.email ||
previous.password != current.password,
listener: (context, state) {
context.read<LoginCubit>().setValid();
context.read<LoginCubit>().validateAllFieldsFilled();
},
),
BlocListener<LoginCubit, LoginState>(
listenWhen: (previous, current) => previous.status != current.status,
listener: (context, state) {
if (state.status.isFailure) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
const SnackBar(
content: Text('Authentication Failure'),
),
);
}
},
),
],
child: Form(
child: Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -56,13 +69,21 @@ class _EmailInput extends StatelessWidget {
onChanged: (email) => context.read<LoginCubit>().emailChanged(email),
keyboardType: TextInputType.emailAddress,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) => validateValue<LoginCubit>([
FormBuilderValidators.required(),
FormBuilderValidators.email(),
],
value,
context,
),
validator: (value) {
final validateResult = validateValue<LoginCubit>(
[
FormBuilderValidators.required(),
FormBuilderValidators.email(),
],
value,
context,
);
if (validateResult != null) {
context.read<LoginCubit>().setInvalid();
}
return validateResult;
},

decoration: InputDecoration(
labelText: LoginStrings.email,
helperText: '',
Expand Down Expand Up @@ -97,13 +118,21 @@ class _PasswordInput extends StatelessWidget {
context.read<LoginCubit>().passwordChanged(password),
obscureText: !state.showPassword,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) => validateValue<LoginCubit>([
FormBuilderValidators.required(),
FormBuilderValidators.minLength(8),
],
value,
context,
),
validator: (value) {
final validateResult = validateValue<LoginCubit>(
[
FormBuilderValidators.required(),
FormBuilderValidators.minLength(8),
],
value,
context,
);
if (validateResult != null) {
context.read<LoginCubit>().setInvalid();
}
return validateResult;
},

decoration: InputDecoration(
labelText: LoginStrings.password,
helperText: '',
Expand Down Expand Up @@ -142,7 +171,7 @@ class _LoginButton extends StatelessWidget {
return state.status.isInProgress
? const CircularProgressIndicator()
: ElevatedButton(
onPressed: state.isValid
onPressed: context.read<LoginCubit>().isLoginButtonEnabled()
? () => context.read<LoginCubit>().logInWithCredentials()
: null,
style: ButtonStyle(
Expand All @@ -152,7 +181,9 @@ class _LoginButton extends StatelessWidget {
),
),
backgroundColor: WidgetStateProperty.all<Color>(
Theme.of(context).colorScheme.primary,
(context.read<LoginCubit>().isLoginButtonEnabled())
? Theme.of(context).colorScheme.primary
: Colors.grey,
),
),
// highlightElevation: 4.0,
Expand Down
Loading