baligh/lib/screens/otp_screen.dart
Abdullah Salah e3ea8335d1 First Commit
2024-12-26 09:05:13 +03:00

141 lines
4.1 KiB
Dart

import 'package:baligh/screens/home_screen.dart';
import 'package:baligh/screens/register_screen.dart';
import 'package:baligh/widgets/app_toast.dart';
import 'package:baligh/widgets/custom_button.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';
class OtpScreen extends StatefulWidget {
const OtpScreen({
super.key,
required this.verificationId,
});
final String verificationId;
@override
State<OtpScreen> createState() => _OtpScreenState();
}
class _OtpScreenState extends State<OtpScreen> {
TextEditingController otpController = TextEditingController();
bool isLoading = false;
@override
void dispose() {
otpController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(),
body: ListView(
padding: const EdgeInsets.all(15),
children: [
const SizedBox(
height: 80,
),
Text(
"تأكيد رقم الهاتف",
style: Theme.of(context).textTheme.headlineLarge,
),
const SizedBox(
height: 10,
),
Text(
"قمنا بإرسال رمز التحقق اليك ادخل رمز التحقق لتأكيد رقم هاتفك",
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(
height: 50,
),
Directionality(
textDirection: TextDirection.ltr,
child: Pinput(
controller: otpController,
length: 6,
autofocus: true,
defaultPinTheme: PinTheme(
width: 56,
height: 56,
textStyle: Theme.of(context)
.textTheme
.bodyLarge
?.copyWith(fontSize: 20),
decoration: BoxDecoration(
color: const Color(0xFFECECEC),
borderRadius: BorderRadius.circular(10),
),
),
separatorBuilder: (index) => const SizedBox(width: 5),
),
),
const SizedBox(
height: 20,
),
CustomButton(
label: "تأكيد رثم الهاتف",
isElevated: true,
onPressed: handleSendOtp,
isLoading: isLoading,
),
],
),
));
}
Future<void> handleSendOtp() async {
if (otpController.text.isEmpty) {
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "يجب ملئ جميع الحقول");
});
}
setState(() {
isLoading = true;
});
try {
// Create a PhoneAuthCredential with the code
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: widget.verificationId,
smsCode: otpController.text.trim());
// Sign the user in (or link) with the credential
UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);
setState(() {
isLoading = false;
});
if (!mounted) return;
if (userCredential.additionalUserInfo?.isNewUser != true) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
} else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const RegisterScreen()),
);
}
} catch (e) {
setState(() {
isLoading = false;
});
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "حدث خطأ ما");
});
}
}
}