import 'package:cheminova/provider/notification_provider.dart'; import 'package:flutter/material.dart'; import 'package:cheminova/widgets/common_background.dart'; import 'package:cheminova/widgets/common_drawer.dart'; import 'package:cheminova/widgets/common_app_bar.dart'; import 'package:cheminova/widgets/common_elevated_button.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import '../models/notification_list_response.dart'; class NotificationScreen extends StatefulWidget { const NotificationScreen({super.key}); @override State createState() => NotificationScreenState(); } class NotificationScreenState extends State { late NotificationProvider _notificationProvider; @override void initState() { _notificationProvider = NotificationProvider(); super.initState(); } // Future _selectDate(BuildContext context) async { // final provider = context.read(); // final dateSelected = await showDatePicker( // context: context, // initialDate: DateTime.now(), // firstDate: DateTime(2000), // lastDate: DateTime(2025), // ); // if (dateSelected != null) { // provider.setDate(DateFormat('dd/MM/yyyy').format(dateSelected)); // } // } Future _selectDate(BuildContext context) async { DateTime? selectedDate = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(2020), // Adjust the start date as needed lastDate: DateTime.now(), ); if (selectedDate != null) { // Format the date to dd/MM/yyyy String formattedDate = DateFormat('dd/MM/yyyy').format(selectedDate); // Call the API with the selected date _notificationProvider.getNotification(formattedDate); } } @override Widget build(BuildContext context) { return ChangeNotifierProvider( create: (context) => _notificationProvider, child: CommonBackground( child: Scaffold( backgroundColor: Colors.transparent, appBar: CommonAppBar( actions: [ IconButton( onPressed: () { Navigator.pop(context); }, icon: Image.asset('assets/Back_attendance.png'), padding: const EdgeInsets.only(right: 20), ), ], title: const Text('Notification', style: TextStyle( fontSize: 20, color: Colors.black, fontWeight: FontWeight.w400, fontFamily: 'Anek')), backgroundColor: Colors.transparent, elevation: 0, bottom: PreferredSize( preferredSize: Size.fromHeight(80), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0), child: TextButton( onPressed: () { _selectDate(context); }, child: Row( children: [ Text("Select Date: ",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 18),), IconButton( icon: const Icon(Icons.calendar_today), onPressed: () { _selectDate(context); }, ), ], ), ), ), ), ), drawer: const CommonDrawer(), body: // Consumer( // // builder: (context, value, child) => value.isLoading // // ? const Center(child: CircularProgressIndicator()) // // : Column( // // children: [ // // Container( // // margin: const EdgeInsets.all(10), // // padding: const EdgeInsets.symmetric(horizontal: 10), // // decoration: const BoxDecoration( // // color: Colors.white, // // borderRadius: BorderRadius.all(Radius.circular(10)), // // ), // // child: Row( // // // mainAxisAlignment: MainAxisAlignment.spaceAround, // // mainAxisAlignment: MainAxisAlignment.center, // // children: [ // // Text( // // value.selectedDate, // // ), // // IconButton( // // onPressed: () { // // _selectDate(context); // // }, // // icon: const Icon(Icons.calendar_month), // // ), // // ], // // ), // // ), // // MyListView(value: value), // // ], // // ), // // ), Consumer( builder: (context, value, child) { if (value.isLoading) { return const Center(child: CircularProgressIndicator()); } else if (value.notificationList.isEmpty) { // Handle empty notification list return const Center(child: Text("No notifications available")); } else { // Show the notification list once data is fetched return MyListView(value: value); } }, ), ), ), ); } } Widget buildProductButton(String productName) { return Padding( padding: const EdgeInsets.only(bottom: 15), child: CommonElevatedButton( borderRadius: 30, width: double.infinity, height: kToolbarHeight - 10, text: productName, backgroundColor: const Color(0xff004791), onPressed: () { // Handle product button press debugPrint('$productName pressed'); }, ), ); } class MyListView extends StatelessWidget { final NotificationProvider value; const MyListView({super.key, required this.value}); @override Widget build(BuildContext context) { Map> groupedNotifications = {}; // Iterate over the notification list and group by formatted date for (var notification in value.notificationList) { String date = DateFormat("dd MMM yyyy").format(DateTime.parse(notification.createdAt ?? '')); if (!groupedNotifications.containsKey(date)) { groupedNotifications[date] = []; } groupedNotifications[date]!.add(notification); } if (groupedNotifications.isEmpty) { return const Center(child: Text("No notifications available")); } return // Expanded( // child: (value.notificationList.isEmpty) // ? const Center( // child: Text( // 'No notifications available', // style: TextStyle(fontSize: 20, color: Colors.white), // ), // ) // ListView.builder( // padding: const EdgeInsets.only(top: 15), // itemCount: value.notificationList.length, // itemBuilder: (context, index) { // return Padding( // padding: // const EdgeInsets.only(bottom: 10, left: 10, right: 10), // child: Padding( // padding: const EdgeInsets.only(bottom: 10), // child: ExpansionTile( // collapsedBackgroundColor: Colors.white, // backgroundColor: Colors.white, // trailing: const SizedBox.shrink(), // title: Text( // value.notificationList[index].title ?? '', // style: const TextStyle( // fontSize: 17, fontWeight: FontWeight.w500), // ), // subtitle: Text(value.notificationList[index].msg ?? ''), // ), // ), // ); // }, // // ); ListView.builder( padding: const EdgeInsets.only(top: 15), itemCount: groupedNotifications.length, // Number of date groups itemBuilder: (context, index) { String date = groupedNotifications.keys.elementAt(index); List notificationsForDate = groupedNotifications[date]!; return Padding( padding: const EdgeInsets.only(bottom: 10, left: 10, right: 10), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Display the date for each group of notifications Padding( padding: const EdgeInsets.only(bottom: 8.0), child: Text( date, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), ), // Display each notification in an expandable tile ...notificationsForDate.map((item) => Padding( padding: const EdgeInsets.only(bottom: 10), child: ExpansionTile( collapsedBackgroundColor: Colors.white, backgroundColor: Colors.white, trailing: const SizedBox.shrink(), // Remove trailing icon title: Text( item.title ?? '', style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w500), ), subtitle: Text(item.msg ?? ''), ), )), ], ), ); }, ); } }