mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 05:37:55 +00:00
Set autovalidate mode to onUserInteraction in the LoginForm widget to improve user experience.
118 lines
4.4 KiB
Dart
118 lines
4.4 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 (value != null) {
|
|
if (value.isEmpty) {
|
|
return 'Please enter your email';
|
|
}
|
|
//Regex for email validation
|
|
if (!RegExp(
|
|
r'^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$')
|
|
.hasMatch(value)) {
|
|
return 'Please enter a valid email';
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
onTapOutside: (event) {
|
|
formKey.currentState!.validate();
|
|
},
|
|
decoration: defaultInputDecoration(context,
|
|
hint: "[email protected]"),
|
|
),
|
|
const SizedBox(height: 10),
|
|
const BodyMedium(
|
|
text: "Password",
|
|
fontColor: Colors.white,
|
|
),
|
|
TextFormField(
|
|
controller: AuthCubit.get(context).passwordController,
|
|
validator: (value) {
|
|
if (value != null) {
|
|
if (value.isNotEmpty) {
|
|
if (value.length < 6) {
|
|
return 'Password must be at least 8 characters';
|
|
}
|
|
} else {
|
|
return 'Please enter your password';
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
onTapOutside: (event) {
|
|
formKey.currentState!.validate();
|
|
},
|
|
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 AuthSuccess,
|
|
isLoading: state is AuthLoading,
|
|
customButtonStyle: ButtonStyle(
|
|
backgroundColor: MaterialStateProperty.all(
|
|
Colors.black.withOpacity(.25),
|
|
),
|
|
foregroundColor: MaterialStateProperty.all(
|
|
Colors.white,
|
|
),
|
|
),
|
|
child: const Text(
|
|
'Login',
|
|
),
|
|
onPressed: () {
|
|
if (formKey.currentState!.validate()) {
|
|
AuthCubit.get(context).login();
|
|
FocusScope.of(context).unfocus();
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|