This commit is contained in:
Mohammed Al-Samarraie
2026-01-13 15:06:59 +03:00
parent fa4bee4771
commit cefd2397fe
8 changed files with 196 additions and 74 deletions

View File

@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../screens/main_screen.dart';
import '../core/di/injection_container.dart';
import '../domain/usecases/login_usecase.dart';
import '../domain/models/login_request.dart';
import '../presentation/blocs/login/login_bloc.dart';
import '../presentation/blocs/login/login_event.dart';
import '../presentation/blocs/login/login_state.dart';
import 'onboarding_button.dart';
class AuthForm extends StatefulWidget {
@@ -16,7 +18,6 @@ class AuthForm extends StatefulWidget {
class _AuthFormState extends State<AuthForm> {
bool _obscure = true;
bool _isLoading = false;
// Text controllers
final TextEditingController _phoneNumberController = TextEditingController();
@@ -26,10 +27,7 @@ class _AuthFormState extends State<AuthForm> {
late FocusNode _phoneNumberFocusNode;
late FocusNode _passwordFocusNode;
// Get LoginUseCase from dependency injection
final LoginUseCase _loginUseCase = sl<LoginUseCase>();
Future<void> _handleLogin() async {
void _handleLogin() {
// Validate inputs
if (_phoneNumberController.text.trim().isEmpty) {
_showError('الرجاء إدخال رقم الهاتف');
@@ -45,48 +43,13 @@ class _AuthFormState extends State<AuthForm> {
_phoneNumberFocusNode.unfocus();
_passwordFocusNode.unfocus();
setState(() {
_isLoading = true;
});
// Dispatch login event
final request = LoginRequest(
phoneNumber: _phoneNumberController.text.trim(),
password: _passwordController.text.trim(),
);
try {
final request = LoginRequest(
phoneNumber: _phoneNumberController.text.trim(),
password: _passwordController.text.trim(),
);
final result = await _loginUseCase(request);
result.fold(
(failure) {
_showError(failure.message);
},
(response) {
if (response.isSuccess) {
// Call the onSubmit callback if provided
if (widget.onSubmit != null) {
widget.onSubmit!();
}
// Navigate to the MainPage
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const MainPage()),
);
} else {
_showError(response.message);
}
},
);
} catch (e) {
_showError('حدث خطأ غير متوقع: $e');
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
context.read<LoginBloc>().add(LoginSubmitted(request));
}
void _showError(String message) {
@@ -139,10 +102,27 @@ class _AuthFormState extends State<AuthForm> {
final horizontalPadding = screenWidth > 600 ? 30.0 : 25.0;
final verticalPadding = screenHeight > 800 ? 38.0 : 28.0;
return Directionality(
textDirection: TextDirection.rtl,
child: FocusScope(
child: Stack(
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state is LoginSuccess) {
// Call the onSubmit callback if provided
if (widget.onSubmit != null) {
widget.onSubmit!();
}
// Navigate to the MainPage
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const MainPage()),
);
} else if (state is LoginError) {
_showError(state.message);
}
},
child: Directionality(
textDirection: TextDirection.rtl,
child: FocusScope(
child: Stack(
alignment: Alignment.center,
children: [
// Border container - decorative element behind the form
@@ -251,12 +231,17 @@ class _AuthFormState extends State<AuthForm> {
SizedBox(height: buttonSpacing), // Responsive spacing
Center(
child: OnboardingButton(
text: _isLoading ? "جاري تسجيل الدخول..." : "تسجيل دخول",
backgroundColor: const Color.fromARGB(239, 35, 87, 74),
onPressed: _isLoading ? null : _handleLogin,
),
BlocBuilder<LoginBloc, LoginState>(
builder: (context, state) {
final isLoading = state is LoginLoading;
return Center(
child: OnboardingButton(
text: isLoading ? "جاري تسجيل الدخول..." : "تسجيل دخول",
backgroundColor: const Color.fromARGB(239, 35, 87, 74),
onPressed: isLoading ? null : _handleLogin,
),
);
},
),
SizedBox(height: bottomSpacing),
@@ -266,6 +251,7 @@ class _AuthFormState extends State<AuthForm> {
],
),
),
),
);
}