Notes
Notes - notes.io |
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
class NotificationsPage extends StatefulWidget {
const NotificationsPage({super.key});
@override
State<NotificationsPage> createState() => _NotificationsPageState();
}
class _NotificationsPageState extends State<NotificationsPage> {
final currentUser = FirebaseAuth.instance.currentUser!;
bool selectionMode = false;
Set<String> selectedIds = {};
bool selectAll = false;
void _toggleSelection(String id) {
setState(() {
if (selectedIds.contains(id)) {
selectedIds.remove(id);
} else {
selectedIds.add(id);
}
});
}
void _toggleSelectAll(List<String> allIds) {
setState(() {
if (selectAll) {
selectedIds.clear();
selectAll = false;
} else {
selectedIds = allIds.toSet();
selectAll = true;
}
});
}
void _confirmDelete() async {
if (selectedIds.isEmpty) return;
final confirmed = await showDialog<bool>(
context: context,
builder: (_) => AlertDialog(
title: const Text("Silmek istediğinize emin misiniz?"),
content: Text("${selectedIds.length} bildirim silinecek."),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text("İptal")),
ElevatedButton(
onPressed: () => Navigator.pop(context, true),
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
child: const Text("Evet, sil"),
),
],
),
);
if (confirmed == true) {
for (String id in selectedIds) {
await FirebaseFirestore.instance
.collection('notifications')
.doc(id)
.delete();
}
setState(() {
selectedIds.clear();
selectionMode = false;
selectAll = false;
});
}
}
void _markAsRead() async {
if (selectedIds.isEmpty) return;
final confirmed = await showDialog<bool>(
context: context,
builder: (_) => AlertDialog(
title: const Text("Okundu olarak işaretle"),
content:
Text("${selectedIds.length} bildirim okundu olarak işaretlenecek."),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text("İptal")),
ElevatedButton(
onPressed: () => Navigator.pop(context, true),
child: const Text("Evet"),
),
],
),
);
if (confirmed == true) {
for (String id in selectedIds) {
await FirebaseFirestore.instance
.collection('notifications')
.doc(id)
.update({'isRead': true});
}
setState(() {
selectedIds.clear();
selectionMode = false;
selectAll = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Notifications"),
backgroundColor: Colors.orange[900],
actions: [
IconButton(
icon: Icon(selectionMode ? Icons.close : Icons.edit),
onPressed: () {
setState(() {
selectionMode = !selectionMode;
selectedIds.clear();
selectAll = false;
});
},
),
],
),
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('notifications')
.where('targetUid', isEqualTo: currentUser.uid)
.orderBy('sentAt', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return const Center(child: CircularProgressIndicator());
final docs = snapshot.data!.docs;
if (docs.isEmpty)
return const Center(child: Text("No notifications yet."));
final allIds = docs.map((doc) => doc.id).toList();
return Column(
children: [
if (selectionMode)
SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding:
const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Row(
children: [
ElevatedButton.icon(
onPressed: () => _toggleSelectAll(allIds),
icon: Icon(selectAll ? Icons.clear : Icons.select_all),
label: Text(selectAll ? "Tümünü Kaldır" : "Tümünü Seç"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange[900]),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: _markAsRead,
icon: const Icon(Icons.mark_email_read),
label: const Text("Okundu Olarak İşaretle"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: _confirmDelete,
icon: const Icon(Icons.delete),
label: const Text("Sil"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red),
),
],
),
),
Expanded(
child: ListView.builder(
itemCount: docs.length,
itemBuilder: (context, index) {
final doc = docs[index];
final data = doc.data() as Map<String, dynamic>;
final id = doc.id;
final title = data['title'] ?? 'No title';
final body = data['message'] ?? '';
final from = data['fromName'] ?? 'Unknown';
final timestamp = (data['sentAt'] as Timestamp?)?.toDate();
final timeString = timestamp != null
? "${timestamp.day}/${timestamp.month}/${timestamp.year} ${timestamp.hour}:${timestamp.minute.toString().padLeft(2, '0')}"
: '';
final isRead = data['isRead'] == true;
return Card(
margin: const EdgeInsets.symmetric(
horizontal: 12, vertical: 8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
side: BorderSide(
color: isRead ? Colors.grey : Colors.orange[900]!,
width: 1.5,
),
),
color: isRead ? Colors.grey[100] : Colors.orange[50],
child: ListTile(
leading: selectionMode
? Checkbox(
value: selectedIds.contains(id),
onChanged: (_) => _toggleSelection(id),
)
: Icon(
isRead
? Icons.notifications_none
: Icons.notifications_active,
color:
isRead ? Colors.grey : Colors.orange[900],
),
title: Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
color: isRead ? Colors.black54 : Colors.black,
),
),
subtitle: Text(
"$bodynFrom: $fromn$timeString",
style: TextStyle(
color: isRead ? Colors.black45 : Colors.black87,
),
),
isThreeLine: true,
onLongPress: () {
setState(() {
selectionMode = true;
_toggleSelection(id);
});
},
onTap: () async {
if (!isRead) {
await FirebaseFirestore.instance
.collection('notifications')
.doc(id)
.update({'isRead': true});
}
},
),
);
},
),
),
],
);
},
),
);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
