import 'package:wanisdriver/Provider/utils_singleton.dart'; 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/utils_provider.dart'; import '../config/constants/app_constant.dart'; import '../config/custom_loader.dart'; import '../services/DriverServices.dart'; import '../services/ride_socket_service_manager.dart'; import '../widgets/CustomTextFieldWidget.dart'; import '../widgets/custom_text.dart'; import 'MainScreen.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 UtilsProvider utilsProvider; late final AuthProvider authProvider; final driverservice driverservices = driverservice(); final emailOrPhoneController = TextEditingController(); final passwordController = TextEditingController(); bool _isLoading = false; @override void initState() { super.initState(); _logout(); // NEW: Call the function to load saved user credentials when the screen starts. _loadSavedCredentials(); } // NEW FUNCTION: To load email and password from device storage. Future _loadSavedCredentials() async { // Get the instance of SharedPreferences. final prefs = await SharedPreferences.getInstance(); // Retrieve the saved email and password. Use '??' to provide a default empty string if null. final String? savedEmail = prefs.getString('Email'); final String? savedPassword = prefs.getString('Password'); // If credentials were found, set them to the text controllers. if (savedEmail != null && savedPassword != null) { setState(() { emailOrPhoneController.text = savedEmail; passwordController.text = savedPassword; }); } } 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); final loginProvider = context.read(); try { final loginProvider = context.read(); final isSuccess = await loginProvider.login(emailOrPhoneController.text,passwordController.text); if (isSuccess) { String token = await driverservices.getToken(); 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) { // if (_isLoading) { // return Container( // width: double.infinity, // height: 55, // decoration: BoxDecoration( // borderRadius: BorderRadius.circular(30), // ), // child: const Center( // child: CustomLoader( // logoSize: 60.0, // dotSize: 10.0, // ), // ), // ); // } return Scaffold( backgroundColor: Theme.of(context).brightness == Brightness.dark ? AppColors.blackBackgroundColor : AppColors.whiteBackgroundColor, resizeToAvoidBottomInset: false, // Prevent the whole page from resizing when the keyboard appears body: Stack( children: [ // Top curve SVG // Positioned( // top: -50, // left: 0, // right: 0, // child: SvgPicture.asset( // "assets/icons/curvTopLoogin.svg", // height: MediaQuery.of(context).size.height * 0.25, // width: MediaQuery.of(context).size.width, // color: Theme.of(context).brightness == Brightness.dark ?AppColors.darkPrimaryColor:AppColors.primaryColor, // )), // Center content (Logo, Login form, etc.) 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, ), ), ), // Positioned( // bottom: -30, // left: 0, // right: 0, // child: SvgPicture.asset( // "assets/icons/curvButtomLoogin.svg", // height: MediaQuery.of(context).size.height * // 0.25, // Adjust the height if needed // width: MediaQuery.of(context).size.width, // 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), // decoration: BoxDecoration( // color: Theme.of(context).brightness == Brightness.dark // ? AppColors.darkCardColor // : AppColors.whiteBackgroundColor, // borderRadius: BorderRadius.circular(20), // boxShadow: const [ // BoxShadow( // color: Colors.black12, // blurRadius: 10, // offset: Offset(0, 5), // ), // ], // ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // const Center( // child: Txt( // text: 'Login', // style: TextStyle( // fontSize: 25, // fontWeight: FontWeight.bold, // fontFamily: 'Poppins', // ), // ), // ), // const SizedBox(height: 20), CustomTextFieldWidget( controller: emailOrPhoneController, icon: Icons.person, label: '', hintText: 'Enter your email or phone number', isPassword: false, keybordType: TextInputType.emailAddress, // Keep this or use TextInputType.text ), 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 Center( child: 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, ), ), ), ), ), ) ], ), ), ], ), ), ], ), // Bottom curve SVG to cover the full bottom ); } }