131 lines
4.7 KiB
Dart
131 lines
4.7 KiB
Dart
import 'package:cheminova/widgets/my_drawer.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:intl/intl.dart';
|
|
import '../../controller/announcement_controller.dart';
|
|
import '../../models/announcement_response.dart';
|
|
import '../../widgets/comman_background.dart';
|
|
import '../../widgets/common_appbar.dart';
|
|
|
|
class AnnouncementScreen extends StatelessWidget {
|
|
final AnnouncementController controller = Get.put(AnnouncementController());
|
|
|
|
AnnouncementScreen({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return CommonBackground(
|
|
child: Scaffold(
|
|
backgroundColor: Colors.transparent,
|
|
appBar: CommonAppBar(
|
|
title: const Text('Announcements',
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
color: Colors.black,
|
|
fontWeight: FontWeight.w400,
|
|
fontFamily: 'Anek')),
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0,
|
|
actions: [
|
|
IconButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
icon: SvgPicture.asset('assets/svg/back_arrow.svg'),
|
|
padding: const EdgeInsets.only(right: 20))
|
|
],
|
|
),
|
|
drawer: const MyDrawer(),
|
|
body: Obx(() => controller.isLoading.value
|
|
? const Center(child: CircularProgressIndicator())
|
|
: NotificationListener<ScrollNotification>(
|
|
onNotification: (ScrollNotification scrollInfo) {
|
|
// Load more announcements when reaching the bottom of the list
|
|
if (!controller.isLoading.value &&
|
|
scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {
|
|
controller.loadMoreAnnouncements();
|
|
}
|
|
return true;
|
|
},
|
|
child: MyListView(announcementList: controller.announcementsList),
|
|
)),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyListView extends StatelessWidget {
|
|
final List<Announcements> announcementList;
|
|
|
|
const MyListView({super.key, required this.announcementList});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Map<DateTime, List<Announcements>> groupedAnnouncements = {};
|
|
|
|
for (var announcement in announcementList) {
|
|
DateTime date = DateUtils.dateOnly(
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(announcement.createdAt!));
|
|
if (!groupedAnnouncements.containsKey(date)) {
|
|
groupedAnnouncements[date] = [];
|
|
}
|
|
groupedAnnouncements[date]!.add(announcement);
|
|
}
|
|
|
|
// Sort the dates in descending order (latest first)
|
|
List<DateTime> sortedDates = groupedAnnouncements.keys.toList()
|
|
..sort((a, b) => b.compareTo(a));
|
|
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.only(top: 15),
|
|
itemCount: sortedDates.length,
|
|
itemBuilder: (context, index) {
|
|
DateTime date = sortedDates[index];
|
|
String formattedDate = DateFormat("dd MMM yyyy").format(date);
|
|
List<Announcements> announcementsForDate = groupedAnnouncements[date]!;
|
|
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 10, left: 10, right: 10),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 8.0),
|
|
child: Text(
|
|
formattedDate,
|
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
...announcementsForDate.map((item) => Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: ExpansionTile(
|
|
collapsedBackgroundColor: Colors.white,
|
|
backgroundColor: Colors.white,
|
|
trailing: const SizedBox.shrink(),
|
|
title: const Text(
|
|
'New Announcement',
|
|
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
|
|
),
|
|
subtitle: Text(item.message ?? ''),
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Sent to: ${item.sentTo?.join(", ")}'),
|
|
const SizedBox(height: 8),
|
|
Text('Unique ID: ${item.uniqueId}'),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
)),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|