import 'package:baligh/screens/otp_screen.dart'; import 'package:baligh/widgets/app_toast.dart'; import 'package:baligh/widgets/custom_button.dart'; import 'package:baligh/widgets/custom_text_field.dart'; import 'package:bot_toast/bot_toast.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({super.key}); @override State createState() => _LoginScreenState(); } class _LoginScreenState extends State { TextEditingController phoneNumberController = TextEditingController(); bool isLoading = false; @override void dispose() { phoneNumberController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Directionality( textDirection: TextDirection.rtl, child: Scaffold( body: SafeArea( child: ListView( padding: const EdgeInsets.all(15), children: [ const SizedBox( height: 150, ), Text( "تسجيل الدخول", style: Theme.of(context).textTheme.headlineLarge, ), const SizedBox( height: 50, ), CustomTextField( controller: phoneNumberController, labelText: "رقم الهاتف", inputType: TextInputType.phone), const SizedBox( height: 15, ), CustomButton( label: "تسجيل الدخول", isElevated: true, onPressed: handleLogIn, isLoading: isLoading, ), ], ), ), ), ); } Future handleLogIn() async { if (phoneNumberController.text.isEmpty) { BotToast.showCustomText(toastBuilder: (_) { return const AppToast(text: "يجب ملئ جميع الحقول"); }); } setState(() { isLoading = true; }); try { await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: "+964${phoneNumberController.text.trim()}", verificationCompleted: (PhoneAuthCredential credential) {}, verificationFailed: (FirebaseAuthException e) { setState(() { isLoading = false; }); BotToast.showCustomText(toastBuilder: (_) { return const AppToast(text: "حدث خطأ ما"); }); }, codeSent: (String verificationId, int? resendToken) { setState(() { isLoading = false; }); Navigator.push( context, MaterialPageRoute( builder: (context) => OtpScreen( verificationId: verificationId, )), ); }, codeAutoRetrievalTimeout: (String verificationId) {}, ); } catch (e) { setState(() { isLoading = false; }); BotToast.showCustomText(toastBuilder: (_) { return const AppToast(text: "حدث خطأ ما"); }); } } }