137 lines
3.9 KiB
Dart
137 lines
3.9 KiB
Dart
import 'package:baligh_dashboard/screens/dashboard_screen.dart';
|
|
import 'package:baligh_dashboard/widgets/app_toast.dart';
|
|
import 'package:baligh_dashboard/widgets/custom_button.dart';
|
|
import 'package:baligh_dashboard/widgets/custom_text_field.dart';
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
TextEditingController usernameController = TextEditingController();
|
|
TextEditingController passwordController = TextEditingController();
|
|
|
|
bool isLoading = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
usernameController.dispose();
|
|
passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Directionality(
|
|
textDirection: TextDirection.rtl,
|
|
child: Scaffold(
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(
|
|
maxWidth: 500,
|
|
minWidth: 100,
|
|
),
|
|
child: ListView(
|
|
shrinkWrap: true,
|
|
padding: const EdgeInsets.all(15),
|
|
children: [
|
|
Text(
|
|
"تسجيل الدخول",
|
|
style: Theme.of(context).textTheme.headlineLarge,
|
|
),
|
|
const SizedBox(
|
|
height: 50,
|
|
),
|
|
CustomTextField(
|
|
controller: usernameController,
|
|
labelText: "اسم المشرف",
|
|
inputType: TextInputType.text),
|
|
const SizedBox(
|
|
height: 10,
|
|
),
|
|
CustomTextField(
|
|
controller: passwordController,
|
|
labelText: "كلمة المرور",
|
|
inputType: TextInputType.text,
|
|
isPassword: true,
|
|
),
|
|
const SizedBox(
|
|
height: 15,
|
|
),
|
|
CustomButton(
|
|
label: "تسجيل الدخول",
|
|
isElevated: true,
|
|
onPressed: handleLogIn,
|
|
isLoading: isLoading,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> handleLogIn() async {
|
|
if (usernameController.text.trim().isEmpty ||
|
|
passwordController.text.trim().isEmpty) {
|
|
BotToast.showCustomText(toastBuilder: (_) {
|
|
return const AppToast(text: "يجب ملئ جميع الحقول");
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
|
|
try {
|
|
final QuerySnapshot snapshot = await FirebaseFirestore.instance
|
|
.collection('admins')
|
|
.where('username', isEqualTo: usernameController.text.trim())
|
|
.where('password', isEqualTo: passwordController.text.trim())
|
|
.get();
|
|
|
|
if (snapshot.docs.isEmpty) {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
BotToast.showCustomText(toastBuilder: (_) {
|
|
return const AppToast(text: "اسم المشرف او كلمة المرور خطأ");
|
|
});
|
|
return;
|
|
}
|
|
|
|
var admin = snapshot.docs.first.data() as Map<String, dynamic>;
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
if (!mounted) return;
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => DashboardScreen(adminRole: admin["role"])),
|
|
);
|
|
} catch (e) {
|
|
print(e);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
BotToast.showCustomText(toastBuilder: (_) {
|
|
return const AppToast(text: "حدث خطأ ما");
|
|
});
|
|
}
|
|
}
|
|
}
|