238 lines
8.1 KiB
Dart
238 lines
8.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'firebase/notification_service.dart';
|
|
|
|
class NotificationController extends GetxController {
|
|
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
|
|
final TextEditingController titleController = TextEditingController();
|
|
final TextEditingController bodyController = TextEditingController();
|
|
|
|
// ألوان الثيم (متناسقة مع باقي الصفحات المحسنة)
|
|
final Color _dialogColor = const Color(0xFF1A1F3A);
|
|
final Color _inputColor = const Color(0xFF0A0E27);
|
|
final Color _primaryAccent = const Color(0xFF6366F1);
|
|
|
|
@override
|
|
void onClose() {
|
|
titleController.dispose();
|
|
bodyController.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
/// تنظيف الحقول
|
|
void _clearFields() {
|
|
titleController.clear();
|
|
bodyController.clear();
|
|
}
|
|
|
|
/// إرسال إشعار للسائقين
|
|
Future<void> sendNotificationDrivers() async {
|
|
_clearFields();
|
|
await _showCustomDialog(
|
|
dialogTitle: 'إشعار للسائقين',
|
|
targetAudience: 'drivers',
|
|
icon: Icons.drive_eta_rounded,
|
|
iconColor: Colors.orangeAccent,
|
|
);
|
|
}
|
|
|
|
/// إرسال إشعار للركاب
|
|
Future<void> sendNotificationPassengers() async {
|
|
_clearFields();
|
|
await _showCustomDialog(
|
|
dialogTitle: 'إشعار للركاب',
|
|
targetAudience: 'passengers',
|
|
icon: Icons.people_alt_rounded,
|
|
iconColor: Colors.blueAccent,
|
|
);
|
|
}
|
|
|
|
/// دالة عامة لإظهار نافذة الإرسال بتصميم عصري
|
|
Future<void> _showCustomDialog({
|
|
required String dialogTitle,
|
|
required String targetAudience,
|
|
required IconData icon,
|
|
required Color iconColor,
|
|
}) {
|
|
return Get.dialog(
|
|
Dialog(
|
|
backgroundColor: _dialogColor,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
|
|
elevation: 10,
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Form(
|
|
key: formKey,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
// 1. أيقونة العنوان
|
|
Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: iconColor.withOpacity(0.15),
|
|
shape: BoxShape.circle,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: iconColor.withOpacity(0.1),
|
|
blurRadius: 10,
|
|
offset: const Offset(0, 4),
|
|
),
|
|
],
|
|
),
|
|
child: Icon(icon, color: iconColor, size: 32),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// 2. العنوان
|
|
Text(
|
|
dialogTitle,
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
fontFamily: 'Segoe UI',
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
// 3. حقول الإدخال
|
|
_buildModernTextField(
|
|
controller: titleController,
|
|
hint: 'عنوان الإشعار',
|
|
icon: Icons.title_rounded,
|
|
),
|
|
const SizedBox(height: 16),
|
|
_buildModernTextField(
|
|
controller: bodyController,
|
|
hint: 'نص الرسالة',
|
|
icon: Icons.message_rounded,
|
|
maxLines: 3,
|
|
),
|
|
const SizedBox(height: 32),
|
|
|
|
// 4. الأزرار
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextButton(
|
|
onPressed: () => Get.back(),
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: Colors.white54,
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
),
|
|
child: const Text('إلغاء'),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: ElevatedButton(
|
|
onPressed: () async {
|
|
if (titleController.text.trim().isEmpty ||
|
|
bodyController.text.trim().isEmpty) {
|
|
Get.snackbar(
|
|
"تنبيه",
|
|
"الرجاء تعبئة جميع الحقول",
|
|
backgroundColor: Colors.amber.withOpacity(0.8),
|
|
colorText: Colors.white,
|
|
snackPosition: SnackPosition.TOP,
|
|
);
|
|
return;
|
|
}
|
|
|
|
Get.back(); // إغلاق النافذة
|
|
await _processSending(targetAudience);
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: _primaryAccent,
|
|
foregroundColor: Colors.white,
|
|
padding: const EdgeInsets.symmetric(vertical: 12),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
elevation: 0,
|
|
),
|
|
child: const Text('إرسال'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
barrierColor: Colors.black.withOpacity(0.7), // تعتيم الخلفية
|
|
);
|
|
}
|
|
|
|
/// تنفيذ عملية الإرسال الفعلية
|
|
Future<void> _processSending(String target) async {
|
|
// إظهار تنبيه بدء العملية
|
|
Get.snackbar(
|
|
"جاري الإرسال",
|
|
"يتم إرسال الإشعار لـ $target...",
|
|
backgroundColor: _primaryAccent.withOpacity(0.2),
|
|
colorText: Colors.white,
|
|
duration: const Duration(seconds: 2),
|
|
);
|
|
|
|
try {
|
|
// استدعاء خدمة الإرسال
|
|
await NotificationService.sendNotification(
|
|
target: target,
|
|
title: titleController.text,
|
|
body: bodyController.text,
|
|
isTopic: true,
|
|
category: 'fromAdmin',
|
|
);
|
|
|
|
Get.snackbar(
|
|
"نجاح",
|
|
"تم إرسال الإشعار بنجاح",
|
|
backgroundColor: Colors.green.withOpacity(0.5),
|
|
colorText: Colors.white,
|
|
);
|
|
} catch (e) {
|
|
Get.snackbar(
|
|
"خطأ",
|
|
"فشل إرسال الإشعار: $e",
|
|
backgroundColor: Colors.red.withOpacity(0.5),
|
|
colorText: Colors.white,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// تصميم حقل الإدخال المخصص (Dark Input Field)
|
|
Widget _buildModernTextField({
|
|
required TextEditingController controller,
|
|
required String hint,
|
|
required IconData icon,
|
|
int maxLines = 1,
|
|
}) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: _inputColor,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: Colors.white.withOpacity(0.1)),
|
|
),
|
|
child: TextField(
|
|
controller: controller,
|
|
style: const TextStyle(color: Colors.white),
|
|
maxLines: maxLines,
|
|
decoration: InputDecoration(
|
|
hintText: hint,
|
|
hintStyle:
|
|
TextStyle(color: Colors.white.withOpacity(0.3), fontSize: 14),
|
|
prefixIcon: Icon(icon, color: Colors.white38, size: 20),
|
|
border: InputBorder.none,
|
|
contentPadding:
|
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|