First Commit

This commit is contained in:
Abdullah Salah
2024-12-26 09:10:35 +03:00
commit cf9784f468
34 changed files with 2278 additions and 0 deletions

View File

@ -0,0 +1,156 @@
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 AdminScreen extends StatefulWidget {
const AdminScreen({super.key});
@override
State<AdminScreen> createState() => _AdminScreenState();
}
class _AdminScreenState extends State<AdminScreen> {
TextEditingController usernameController = TextEditingController();
TextEditingController passwordController = TextEditingController();
String selectedRole = "التحرش";
List<String> availableRoles = [
"التحرش",
"التعنيف",
"الابتزاز",
"المخدرات",
"الرشوة",
];
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: 10,
),
Row(
children: [
DropdownMenu<String>(
initialSelection: "التحرش",
label: const Text("نوع البلاغ"),
onSelected: (String? value) {
setState(() {
selectedRole = value ?? "التحرش";
});
},
dropdownMenuEntries: availableRoles
.map<DropdownMenuEntry<String>>((value) {
return DropdownMenuEntry<String>(
value: value,
label: value,
);
}).toList(),
),
],
),
const SizedBox(
height: 15,
),
CustomButton(
label: "اضافة المشرف",
isElevated: true,
onPressed: handleCreateAdmin,
isLoading: isLoading,
),
],
),
),
),
),
),
);
}
Future<void> handleCreateAdmin() async {
if (passwordController.text.trim().isEmpty ||
usernameController.text.trim().isEmpty) {
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "يجب ملئ جميع الحقول");
});
return;
}
setState(() {
isLoading = true;
});
try {
await FirebaseFirestore.instance.collection("admins").add({
"role": selectedRole,
"username": usernameController.text.trim(),
"password": passwordController.text.trim(),
"createdAt": FieldValue.serverTimestamp(),
});
setState(() {
isLoading = false;
usernameController.clear();
passwordController.clear();
});
// if (!mounted) return;
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "تم اضافة مشرف");
});
} catch (e) {
print(e);
setState(() {
isLoading = false;
});
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "حدث خطأ ما");
});
}
}
}

View File

@ -0,0 +1,245 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:baligh_dashboard/screens/admin_screen.dart';
import 'package:baligh_dashboard/widgets/custom_button.dart';
import 'package:baligh_dashboard/widgets/dashboard_tile.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class DashboardScreen extends StatefulWidget {
const DashboardScreen({super.key, required this.adminRole});
final String adminRole;
@override
State<DashboardScreen> createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen> {
bool isLoading = false;
String? error;
List<Map<String, dynamic>> reports = [];
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
getInitialData();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
height: 50,
),
AutoSizeText(
"البلاغات",
style: Theme.of(context).textTheme.titleLarge,
minFontSize: 12,
),
if (isLoading) ...[
const SizedBox(
height: 50,
),
Text("جار التحميل",
style: Theme.of(context).textTheme.bodyLarge),
] else if (error != null) ...[
const SizedBox(
height: 50,
),
Text(error!, style: Theme.of(context).textTheme.bodyLarge),
] else ...[
if (widget.adminRole == "admin") ...[
const SizedBox(
height: 10,
),
SizedBox(
width: 200,
child: CustomButton(
label: "اضافة مشرف",
isElevated: true,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AdminScreen()));
},
),
),
],
const SizedBox(
height: 30,
),
Row(
children: [
Expanded(
child: AutoSizeText(
"اسم صاحب البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"عنوان صاحب البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"المدرسة",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"نوع البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"الحالة",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
],
),
const SizedBox(
height: 10,
),
Expanded(
child: ListView.separated(
shrinkWrap: true,
itemCount: reports.length,
separatorBuilder: (context, index) => const SizedBox(
height: 10,
),
itemBuilder: (context, index) => DashboardTile(
id: reports[index]["id"] ?? "",
name: reports[index]["name"] ?? "",
age: reports[index]["age"] ?? "",
phone: reports[index]["phone"] ?? "",
address: reports[index]["address"] ?? "",
school: reports[index]["schoolName"] ?? "",
nationalIdNumber:
reports[index]["nationalIdNumber"] ?? "",
description: reports[index]["description"] ?? "",
attachments: reports[index]["attachments"] ?? "",
type: reports[index]["type"] ?? "",
createdAt: reports[index]["createdAt"] ?? "",
status: reports[index]["status"] ?? ""),
),
)
]
],
),
),
),
),
);
}
Future<void> getInitialData() async {
try {
setState(() {
isLoading = true;
});
var temp = await getReportsWithUsers();
setState(() {
isLoading = false;
reports = temp;
});
} catch (e, s) {
setState(() {
isLoading = false;
error = "حدث خطأ ما";
});
}
}
Future<List<Map<String, dynamic>>> getReportsWithUsers() async {
QuerySnapshot reportsSnapshot;
if (widget.adminRole == "admin") {
reportsSnapshot = await FirebaseFirestore.instance
.collection('reports')
.orderBy('createdAt', descending: true)
.get();
} else {
reportsSnapshot = await FirebaseFirestore.instance
.collection('reports')
.where('type', isEqualTo: widget.adminRole)
.orderBy('createdAt', descending: true)
.get();
}
List<Map<String, dynamic>> reportsWithUsers = [];
Set<DocumentReference> userRefs = {};
// Collect unique user references
for (var reportDoc in reportsSnapshot.docs) {
var reportData = reportDoc.data() as Map<String, dynamic>;
if (reportData['userRef'] == null) continue;
userRefs.add(reportData['userRef'] as DocumentReference);
reportsWithUsers.add({
'id': reportDoc.id,
'type': reportData["type"],
'description': reportData["description"],
'attachments': reportData["attachments"],
'status': reportData["status"],
'createdAt': reportData["createdAt"],
'userId': reportData["userId"],
});
}
// Fetch all user data in one go
var userDocs = await Future.wait(userRefs.map((ref) => ref.get()));
for (var el in userDocs) {
int index = reportsWithUsers.indexWhere((r) => r["userId"] == el["uid"]);
reportsWithUsers[index]["name"] = el["name"];
reportsWithUsers[index]["phone"] = el["phone"];
reportsWithUsers[index]["age"] = el["age"];
reportsWithUsers[index]["schoolName"] = el["schoolName"];
reportsWithUsers[index]["stage"] = el["stage"];
reportsWithUsers[index]["address"] = el["address"];
reportsWithUsers[index]["nationalIdNumber"] = el["nationalIdNumber"];
}
return reportsWithUsers;
}
}

View File

@ -0,0 +1,136 @@
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: "حدث خطأ ما");
});
}
}
}

View File

@ -0,0 +1,244 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:baligh_dashboard/utils/file_classification.dart';
import 'package:baligh_dashboard/widgets/app_toast.dart';
import 'package:baligh_dashboard/widgets/dashboard_tile.dart';
import 'package:baligh_dashboard/widgets/file_card.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class ReportDetailsScreen extends StatefulWidget {
const ReportDetailsScreen({
super.key,
required this.id,
required this.name,
required this.address,
required this.school,
required this.type,
required this.status,
required this.age,
required this.attachments,
required this.createdAt,
required this.description,
required this.nationalIdNumber,
required this.phone,
});
final String id;
final String name;
final String address;
final String school;
final String type;
final String status;
final Timestamp createdAt;
final int age;
final String phone;
final int nationalIdNumber;
final String description;
final List attachments;
@override
State<ReportDetailsScreen> createState() => _ReportDetailsScreenState();
}
class _ReportDetailsScreenState extends State<ReportDetailsScreen> {
String selectedStatus = "قيد الانتظار";
List<String> availableStatus = [
"قيد الانتظار",
"قيد المعالجة",
"مكتمل",
];
@override
void initState() {
setState(() {
selectedStatus = widget.status;
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: SafeArea(
child: ListView(
padding: const EdgeInsets.symmetric(horizontal: 15),
children: [
const SizedBox(
height: 50,
),
AutoSizeText(
"تفاصيل البلاغ",
style: Theme.of(context).textTheme.titleLarge,
minFontSize: 12,
),
const SizedBox(
height: 30,
),
Wrap(
children: [
Text(
"حالة البلاغ:",
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(
width: 5,
),
DropdownMenu<String>(
initialSelection: "قيد الانتظار",
label: const Text("الحالة"),
onSelected: (String? value) async {
var old = selectedStatus;
setState(() {
selectedStatus = value ?? "قيد الانتظار";
});
try {
await FirebaseFirestore.instance
.collection("reports")
.doc(widget.id)
.update({
'status': selectedStatus,
});
} catch (e) {
setState(() {
selectedStatus = old;
});
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "حدث خطأ ما");
});
}
},
dropdownMenuEntries:
availableStatus.map<DropdownMenuEntry<String>>((value) {
return DropdownMenuEntry<String>(
value: value,
label: value,
);
}).toList(),
),
],
),
const SizedBox(
height: 15,
),
Row(
children: [
Expanded(
child: AutoSizeText(
"اسم صاحب البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"عنوان صاحب البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"المدرسة",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"نوع البلاغ",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
const SizedBox(
width: 5,
),
Expanded(
child: AutoSizeText(
"الحالة",
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
minFontSize: 8,
),
),
],
),
const SizedBox(
height: 10,
),
DashboardTile(
id: widget.id,
name: widget.name,
age: widget.age,
phone: widget.phone,
address: widget.address,
school: widget.school,
nationalIdNumber: widget.nationalIdNumber,
description: widget.description,
attachments: widget.attachments,
type: widget.type,
createdAt: widget.createdAt,
status: widget.status,
),
const SizedBox(
height: 20,
),
AutoSizeText(
"وصف البلاغ",
style: Theme.of(context).textTheme.bodyLarge,
minFontSize: 12,
),
const SizedBox(
height: 10,
),
Text(
widget.description,
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(
height: 20,
),
AutoSizeText(
"الملفات المرفقة",
style: Theme.of(context).textTheme.bodyLarge,
minFontSize: 12,
),
const SizedBox(
height: 10,
),
Wrap(
children: widget.attachments
.map(
(el) => FileCard(
type: FileClassification.getFileClass(el),
filename: FileClassification.getFileName(el) ?? "",
fileUrl: el,),
)
.toList(),
),
const SizedBox(
height: 30,
),
],
),
),
),
);
}
}