Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anouncement #237

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/new_ui/screens/AnnouncementScreen/announcementmodel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:cloud_firestore/cloud_firestore.dart';

class AnnouncementModel {
Timestamp? startDate;
Timestamp? endDate;
String? div;
String? branch;
String? batch;
String? docURL;
String? title;

AnnouncementModel(
{this.startDate,
this.endDate,
this.div,
this.branch,
this.batch,
this.docURL,
this.title});

AnnouncementModel.fromJson(Map<String, dynamic> json) {
startDate = json['startDate'];
endDate = json['endDate'];
div = json['div'];
branch = json['branch'];
batch = json['batch'];
docURL = json['docURL'];
title = json['title'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['startDate'] = this.startDate;
data['endDate'] = this.endDate;
data['div'] = this.div;
data['branch'] = this.branch;
data['batch'] = this.batch;
data['docURL'] = this.docURL;
data['title'] = this.title;
return data;
}
}
117 changes: 117 additions & 0 deletions lib/new_ui/screens/AnnouncementScreen/announcementscreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

// import '../models/announcement_model/announcement_model.dart';
import 'announcementmodel.dart';

class AnnouncementScreen extends StatelessWidget {
final DateTime _lastDate = DateTime.now();

@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverToBoxAdapter(
// You can add a header here if needed
),
],
body: StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance.collection('ImportantNotice').doc('Content').snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(
child: Text(
'Error: ${snapshot.error}',
style: TextStyle(color: Colors.white),
),
);
} else if (!snapshot.hasData || !snapshot.data!.exists) {
return Center(
child: Text(
'No data available',
style: TextStyle(color: Colors.white),
),
);
} else {
final data = snapshot.data!.data() as Map<String, dynamic>?;
final List<dynamic> announcementsData = data?['content'] ?? [];

print("in announcement screen ppppppppppppppppppppppppppppppppppppppp");
print(data);

final announcements = announcementsData.map((json) => AnnouncementModel.fromJson(json)).toList();

return ListView.builder(
itemCount: announcements.length,
itemBuilder: (context, index) {
final announcement = announcements[index];
final listTile = AnnouncementListItem(
announcementModel: announcement,
);

if (_lastDate.isSameDate(_parseTimestamp(announcement.startDate))) {
return listTile;
}

return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildDateHeader(_parseTimestamp(announcement.startDate)),
listTile,
],
);
},
);
}
},
),
),
);
}

DateTime _parseTimestamp(dynamic timestamp) {
if (timestamp is Timestamp) {
return timestamp.toDate();
} else if (timestamp is String) {
return DateTime.parse(timestamp);
} else {
return DateTime.now();
}
}

Widget _buildDateHeader(DateTime date) {
// Customize this method to build your date header
return Container(
padding: EdgeInsets.all(8.0),
color: Colors.grey,
child: Text(
"${date.toLocal()}".split(' ')[0], // Display the date in a readable format
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
);
}
}

class AnnouncementListItem extends StatelessWidget {
final AnnouncementModel announcementModel;

AnnouncementListItem({required this.announcementModel});

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(announcementModel.title ?? "No Title"),
subtitle: Text(announcementModel.docURL ?? "No URL"),
// Add other fields as needed
);
}
}

extension DateTimeExtensions on DateTime {
bool isSameDate(DateTime other) {
return this.year == other.year && this.month == other.month && this.day == other.day;
}
}
Loading