import 'dart:developer'; import 'dart:developer'; 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 '../../config/custom_text.dart'; import '../../config/language/language_enum.dart'; import '../../providers/authentification_provider.dart'; import '../../providers/language_provider.dart'; import '../../providers/utils_singleton.dart'; import '../../routes/app_routes.dart'; import '../../services/Sockets/PubNubManager.dart'; import '../../utils/colors.dart'; import '../../widgets/custom_button.dart'; enum VerificationMethod { sms, whatsapp } class SelectVerificationReceiveScreen extends StatefulWidget { final String phoneNumber; final bool isForgetPassword; const SelectVerificationReceiveScreen({super.key, required this.phoneNumber, required this.isForgetPassword}); @override State createState() => _SelectVerificationReceiveScreenState(); } class _SelectVerificationReceiveScreenState extends State { VerificationMethod selectedMethod = VerificationMethod.sms; late final AuthProvider authProvider; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { authProvider = context.read(); Provider.of(context, listen: false).getLanguage(); // context.read().dispose(); }); } Future _isUserTokenValid(SharedPreferences prefs) async { var tokenData = prefs.getString("token"); bool tokenverif = tokenData != null && tokenData.isNotEmpty && tokenData != "InvalidToken"; utils.updatetoken(tokenData ?? "InvalidToken"); bool fetchprofile = await authProvider.fetchProfile(); return fetchprofile && tokenverif; } @override Widget build(BuildContext context) { final languageProvider = context.watch(); final signUpProvider = context.watch(); bool isArabic = languageProvider.currentLangCode == Language.ar; bool isFrench = languageProvider.currentLangCode == Language.fr; return Scaffold( backgroundColor: AppNewColors.whiteBackGround, body: Padding( padding: const EdgeInsets.symmetric(horizontal: 24.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const SizedBox(height: 60), InkWell( onTap: () { Navigator.pop(context); }, child: Container( width: 32, height: 32, margin: const EdgeInsets.symmetric(vertical: 12), decoration: BoxDecoration( color: AppNewColors.lightgray, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: AppNewColors.black.withOpacity(.1), blurRadius: 1, offset: const Offset(2, 1), ) ]), child: const Icon( Icons.arrow_back, color: AppNewColors.black, size: 16, ), ), ), const SizedBox(height: 24), SizedBox( width: double.infinity, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Txt( text: isArabic ? 'التحقق من رقم الهاتف' : isFrench ? 'Vérification du numéro de téléphone' : 'Phone Number Verification', style: TextStyle( color: AppNewColors.titleTextColor, fontSize: 26, fontFamily: isArabic ? "Readex_Pro" : 'Inter', fontWeight: FontWeight.w600, height: 1.23, ), ), const SizedBox(height: 4), Txt( text: isArabic ? 'سوف نرسل رمز التحقق إلى رقمك +${widget.phoneNumber}' : isFrench ? 'Nous vous enverrons un code de vérification à votre numéro +${widget.phoneNumber}' : 'We will send a Verification code to your number +${widget.phoneNumber}', style: TextStyle( color: AppNewColors.grayTxtColor, fontSize: 18, fontFamily: isArabic ? "Readex_Pro" : 'Inter', fontWeight: FontWeight.w500, height: 1.33, ), ), ], ), ), const SizedBox(height: 32), Txt( text: isArabic ? 'اختر الطريقة التي تريد استلام رمزك بها' : isFrench ? 'Choisissez la méthode de réception de votre code' : 'Choose which method you want to receive your code', style: TextStyle( color: AppNewColors.titleTextColor, fontSize: 16, fontFamily: isArabic ? "Readex_Pro" : 'Inter', fontWeight: FontWeight.w500, height: 1.50, ), ), const SizedBox(height: 16), // SMS Option _buildVerificationOption( method: VerificationMethod.sms, svg: 'assets/images/New/message.svg', title: isArabic ? 'الحصول على الرمز عبر الرسائل النصية' : isFrench ? 'Recevoir le code par SMS' : 'Get code with SMS', isArabic: isArabic, enabled: true, ), const SizedBox(height: 12), // WhatsApp Option _buildVerificationOption( method: VerificationMethod.whatsapp, svg: 'assets/images/New/whatsApp.svg', title: isArabic ? 'الحصول على الرمز عبر واتساب' : isFrench ? 'Recevoir le code par WhatsApp' : 'Get code with WhatsApp', isArabic: isArabic, enabled: true, // 👈 disables it ), const SizedBox(height: 24), CustomButton( onClick: () async { final prefs = await SharedPreferences.getInstance(); print('Complete Phone Number: ${widget.phoneNumber}'); var res = false; if (widget.isForgetPassword){ if(selectedMethod == VerificationMethod.whatsapp){ res = await signUpProvider.phoneForgetPasswordSendWithWhatsapp(widget.phoneNumber); }else{ res = await signUpProvider.phoneForgetPassword(widget.phoneNumber); } }else{ if(selectedMethod == VerificationMethod.whatsapp){ res = await signUpProvider.phoneSignUPOtpWhatsapp(widget.phoneNumber); }else{ res = await signUpProvider.phoneSignUp(widget.phoneNumber); } } if (res) { prefs.setString("token", ""); Get.toNamed(AppRoutes.verifOtp, arguments: { 'phoneNumber': widget.phoneNumber, 'isForgetPassword':widget.isForgetPassword}, ); } }, fontSize: 18, width: double.infinity, height: 40, borderRadius: 10, txtColor: AppNewColors.white, title: isArabic ? 'إرسال الرمز' : isFrench ? 'Envoyer le code' : 'Send code', backgroundColor: AppNewColors.primaryBtnColor, borderColor: AppNewColors.primaryBtnColor, isloading: signUpProvider.loader, loadercolor: AppNewColors.white, ), ], ), ), ); } Widget _buildVerificationOption({ required VerificationMethod method, required String svg, required String title, required bool isArabic, Color iconColor = Colors.grey, bool enabled = true, // new param }) { bool isSelected = selectedMethod == method; return GestureDetector( onTap: enabled ? () { setState(() { selectedMethod = method; log("selectedMethod $selectedMethod"); }); } : null, // disable tap child: Opacity( opacity: enabled ? 1.0 : 0.4, // gray out if disabled child: Row( children: [ Container( width: 24, height: 24, decoration: BoxDecoration( color: AppNewColors.lightgray, borderRadius: BorderRadius.circular(20), ), child: SvgPicture.asset( svg, width: 16, height: 16, //color: enabled ? null : Colors.grey, // force gray icon if disabled ), ), const SizedBox(width: 16), Expanded( child: Txt( text: title, style: TextStyle( color: enabled ? const Color(0xFF5B616D) : Colors.grey, // gray text if disabled fontSize: 16, fontFamily: isArabic ? "Readex_Pro" : 'Inter', fontWeight: FontWeight.w500, height: 1.50, ), ), ), Radio( value: method, groupValue: selectedMethod, onChanged: enabled ? (VerificationMethod? value) { if (value != null) { setState(() { log("selectedMethod $selectedMethod"); selectedMethod = value; log("selectedMethod $selectedMethod"); }); } } : null, // disable Radio activeColor: AppNewColors.primaryColor, ), ], ), ), ); } }