Files
syncrow-app/lib/features/auth/view/widgets/login/login_form.dart
Mohammad Salameh cfc395e210 Refactor HTTPInterceptor and add CustomSnackBar helper
Refactor HTTPInterceptor to handle error responses and add a CustomSnackBar
helper to display snack bars. This will improve error handling and user
feedback in the application.
2024-04-15 12:02:34 +03:00

146 lines
5.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/auth/view/widgets/login/forget_password.dart';
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';
class LoginForm extends StatelessWidget {
const LoginForm({
super.key,
});
@override
Widget build(BuildContext context) {
var formKey = GlobalKey<FormState>();
return BlocBuilder<AuthCubit, AuthState>(
builder: (context, state) {
return Form(
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const BodyMedium(
text: "Email",
fontColor: Colors.white,
),
TextFormField(
controller: AuthCubit.get(context).emailController,
validator: (value) {
if (state is! AuthTokenError) {
if (value != null) {
if (value.isNotEmpty) {
if (RegExp(
r'^[a-zA-Z0-9._-]+@{1}[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$')
.hasMatch(value)) {
return null;
} else {
return 'Please enter a valid email';
}
} else {
return 'Please enter your email';
}
} else {
return 'Please enter your email';
}
}
return null;
},
onTapOutside: (event) {
FocusScope.of(context).unfocus();
},
decoration: defaultInputDecoration(context,
hint: "Example@email.com"),
),
const SizedBox(height: 10),
const BodyMedium(
text: "Password",
fontColor: Colors.white,
),
TextFormField(
controller: AuthCubit.get(context).passwordController,
validator: (value) {
if (state is! AuthTokenError) {
if (value != null) {
if (value.isNotEmpty) {
return null;
//TODO: uncomment this when the backend is ready
// if (value.length > 8) {
// if (RegExp(
// r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
// .hasMatch(value)) {
// return null;
// } else {
// return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character';
// }
// } else {
// return 'Password must be at least 8 characters';
// }
} else {
return 'Please enter your password';
}
}
}
return null;
},
onTapOutside: (event) {
FocusScope.of(context).unfocus();
},
obscureText: !AuthCubit.get(context).isPasswordVisible,
decoration: defaultInputDecoration(context,
hint: "At least 8 characters"),
),
const SizedBox(height: 10),
// const LoginUserAgreement(),
const ForgetPassword(),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: DefaultButton(
isDone: state is AuthLoginSuccess,
isLoading: state is AuthLoading,
// enabled: AuthCubit.get(context)
// .emailController
// .text
// .isNotEmpty &&
// AuthCubit.get(context)
// .passwordController
// .text
// .isNotEmpty,
customButtonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Colors.black.withOpacity(.25),
),
foregroundColor: MaterialStateProperty.all(
Colors.white,
),
),
child: const Text(
'Login',
),
onPressed: () {
if (formKey.currentState!.validate()) {
if (state is! AuthLoginLoading) {
AuthCubit.get(context).login();
FocusScope.of(context).unfocus();
}
}
},
),
),
],
)
],
),
),
);
},
);
}
}