import 'package:wanisdriver/config/AppSnackbar.dart'; import 'package:wanisdriver/utils/colors.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:get/get.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../Provider/authentification_provider.dart'; import '../Provider/public_keys_provider.dart'; import '../Provider/utils_provider.dart'; import '../services/DriverServices.dart'; import '../services/ride_socket_service_manager.dart'; import '../widgets/CustomTextFieldWidget.dart'; import '../widgets/custom_text.dart'; import '../widgets/missing_keys_banner.dart'; import 'Password/changePasswordFirstStepScreen.dart'; import 'SignUpScreenDriver.dart'; import 'location_screen.dart'; class LoginScreen extends StatefulWidget { @override _LoginScreenState createState() => _LoginScreenState(); } class _LoginScreenState extends State { late final AuthProvider authProvider; final driverservice driverservices = driverservice(); final emailOrPhoneController = TextEditingController(); final passwordController = TextEditingController(); bool _isLoading = false; @override void initState() { super.initState(); _logout(); _loadSavedCredentials(); } Future _loadSavedCredentials() async { final prefs = await SharedPreferences.getInstance(); final String? savedEmail = prefs.getString('Email'); final String? savedPassword = prefs.getString('Password'); if (savedEmail != null && savedPassword != null) { setState(() { emailOrPhoneController.text = savedEmail; passwordController.text = savedPassword; }); } } // ── fill test credentials in one tap ───────────────────────── void _fillTestCredentials() { setState(() { emailOrPhoneController.text = 'hr@camel-soft.com'; passwordController.text = 'aze'; }); } void _logout() async { await RideSocketServiceManager.stopService(); } void _setLoading(bool value) { if (_isLoading != value) setState(() => _isLoading = value); } @override void dispose() { emailOrPhoneController.dispose(); passwordController.dispose(); super.dispose(); } Future _handleLogin() async { if (_isLoading) return; _setLoading(true); try { final loginProvider = context.read(); final isSuccess = await loginProvider.login( emailOrPhoneController.text, passwordController.text); if (isSuccess) { await publickeys.loadSecretKeys(); Get.offAll(() => const AllowLocationScreen()); } else { AppSnackbar.showError( "Login Failed", "Invalid credentials. Please try again."); } } catch (e) { AppSnackbar.showError( "Error", "An error occurred. Please try again later."); } finally { _setLoading(false); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Theme.of(context).brightness == Brightness.dark ? AppColors.blackBackgroundColor : AppColors.whiteBackgroundColor, resizeToAvoidBottomInset: false, body: Column( children: [ // ── banners at very top ─────────────────────────────── const MissingKeysBanner(), if (publickeys.isTestEnvironment) Container( width: double.infinity, color: Colors.orange.shade50, padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Row(children: [ Icon(Icons.science, color: Colors.orange, size: 16), SizedBox(width: 6), Text('Test Mode', style: TextStyle( color: Colors.orange, fontWeight: FontWeight.bold, fontSize: 13)), ]), const SizedBox(height: 4), GestureDetector( onTap: _fillTestCredentials, child: Container( padding: const EdgeInsets.symmetric( vertical: 4, horizontal: 8), decoration: BoxDecoration( color: Colors.orange.shade100, borderRadius: BorderRadius.circular(6), ), child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.touch_app, size: 14, color: Colors.orange), SizedBox(width: 4), Text('Tap: hr@camel-soft.com / aze', style: TextStyle( color: Colors.deepOrange, fontSize: 12, fontWeight: FontWeight.w500)), ], ), ), ), if (publickeys.missingPublicKeys.isNotEmpty) Text('🔑 Not configured: ${publickeys.missingPublicKeys.join(', ')}', style: TextStyle( color: Colors.red.shade700, fontSize: 11)), if (publickeys.missingPublicKeys.isEmpty) const Text('✅ All public keys configured', style: TextStyle(color: Colors.green, fontSize: 11)), ], ), ), // ── original Stack content unchanged ────────────────── Expanded( child: Stack( children: [ Positioned( top: MediaQuery.of(context).size.height * 0.17, left: 0, right: 0, child: Center( child: SvgPicture.asset( "assets/images/logo.svg", height: MediaQuery.of(context).size.width * .25, width: MediaQuery.of(context).size.width * .25, color: AppColors.primaryColor, ), ), ), Padding( padding: EdgeInsets.only( top: MediaQuery.of(context).size.height * 0.28, left: 25.0, right: 25.0, ), child: Column( children: [ Container( padding: const EdgeInsets.symmetric( horizontal: 15, vertical: 30), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ CustomTextFieldWidget( controller: emailOrPhoneController, icon: Icons.person, label: '', hintText: 'Enter your email or phone number', isPassword: false, keybordType: TextInputType.emailAddress, ), const SizedBox(height: 20), CustomTextFieldWidget( controller: passwordController, label: '', icon: Icons.lock_outline, isPassword: true, keybordType: TextInputType.text, ), const SizedBox(height: 15), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row(children: [ Checkbox( value: true, onChanged: (value) {}, checkColor: AppColors.white, activeColor: Theme.of(context).brightness == Brightness.dark ? AppColors.darkPrimaryColor : AppColors.primaryColor, ), const Txt( text: 'Remember me', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontFamily: 'Poppins', fontSize: 10, fontWeight: FontWeight.w500), ), ]), GestureDetector( onTap: () => Navigator.push( context, MaterialPageRoute( builder: (context) => const ChangePasswordFirstStepScreen())), child: const Txt( text: 'Forgot your password?', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xFF757575), fontFamily: 'Poppins', fontSize: 12, fontWeight: FontWeight.bold), ), ), ], ), const SizedBox(height: 15), Center( child: InkWell( onTap: _isLoading ? null : _handleLogin, child: Container( width: double.infinity, height: 55, decoration: BoxDecoration( color: _isLoading ? Colors.grey : Theme.of(context).brightness == Brightness.dark ? AppColors.darkPrimaryColor : AppColors.primaryColor, borderRadius: BorderRadius.circular(30), ), child: Center( child: _isLoading ? const CircularProgressIndicator( color: Colors.white) : const Txt( text: 'log in', style: TextStyle( fontFamily: 'Poppins', fontWeight: FontWeight.w600, fontSize: 18, color: Colors.white)), ), ), ), ), const SizedBox(height: 10), Center( child: InkWell( onTap: _isLoading ? null : () => Navigator.push( context, MaterialPageRoute( builder: (context) => const SignUpScreenDriver())), child: Container( width: double.infinity, height: 55, decoration: BoxDecoration( color: _isLoading ? Colors.grey : Theme.of(context).brightness == Brightness.dark ? AppColors.darkPrimaryColor : AppColors.primaryColor, borderRadius: BorderRadius.circular(30), ), child: const Center( child: Txt( text: 'Sign Up', style: TextStyle( fontFamily: 'Poppins', fontWeight: FontWeight.w600, fontSize: 18, color: Colors.white), ), ), ), ), ), ], ), ), ], ), ), ], ), ), ], ), ); } }