notification count added in menu

This commit is contained in:
Surendiran 2025-04-18 16:45:16 +05:30
parent 4c5d95abd9
commit 967fd2fd7a

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:http/http.dart' as http;
import 'package:iconify_design/iconify_design.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@ -88,6 +89,10 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
static final _repo = getIt.call<TAuthRepo>();
late final Locale locale;
List<Map<String, dynamic>> notifications = [];
List<String> userReadedNotiOrNot = [];
int notificationCount = 0;
@override
void initState() {
super.initState();
@ -703,6 +708,12 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
String recordId = userId;
String collectionId =
userDetailsResponse.data['collectionId'] ?? '_pb_users_auth_';
List<dynamic> userReadedNotification = userDetailsResponse.data['pushed_notification'];
// Convert to List<String>
userReadedNotiOrNot = List<String>.from(userReadedNotification);
print('userReadedNotiOrNot: $userReadedNotiOrNot');
if (userAvatar!.isNotEmpty && recordId.isNotEmpty) {
_avatarUrl = '$apiUrl/api/files/$collectionId/$recordId/$userAvatar';
@ -710,6 +721,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
_avatarUrl = ''; // Reset to default or empty
}
});
fetchNotifications(locale?.languageCode ?? 'en');
// **Set Default Locale in Provider**
// if (preferredLanguage == 'ar') {
// ref.read(localeProvider.notifier).setLocale(const Locale('ar'));
@ -721,6 +733,70 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
}
}
Future<void> fetchNotifications(locale) async {
final baseUrl = apiUrl + '/api/getNotification';
try {
final response = await http.get(Uri.parse('$baseUrl?language=$locale'));
if (response.statusCode == 200) {
final Map<String, dynamic> jsonResponse =
jsonDecode(response.body); // Decode JSON
if (jsonResponse['message'] == 'Success') {
setState(() {
notifications = List<Map<String, dynamic>>.from(
jsonResponse['data']); // Assign decoded data
print('notifications $notifications');
// isLoading = false;
});
_processNotifications(notifications , userReadedNotiOrNot);
} else {
throw Exception('Failed to load data');
}
} else {
throw Exception(
'Failed to load data with status code ${response.statusCode}');
}
} catch (e) {
print('Error fetching data: $e');
}
}
void _processNotifications(List<Map<String, dynamic>> notification, List<String> push) {
print('inside last function ${notification.length}');
print('inside last function ${push.length}');
int count = 0;
for (var product in notification) {
if (push.contains(product['id'])) {
count++;
}
}
print('count is $count');
notificationCount = count;
}
Widget _buildNotificationBadge({required int count}) {
if (count == 0) return SizedBox.shrink();
return Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(50),
),
child: Text(
'$count',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
);
}
// Future<void> logout() async {
// final prefs = await SharedPreferences.getInstance();
// // setState(() {
@ -788,7 +864,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
scrolledUnderElevation: 0,
titleSpacing: 0,
backgroundColor: widget.appbarColor ?? Colors.white,
leadingWidth: widget.showBackButton ? mywidth * 0.20 : null,
leadingWidth: widget.showBackButton ? mywidth * 0.18 : null,
bottom: widget.showDivider
? PreferredSize(
preferredSize: Size.fromHeight(1), // Height of the line
@ -828,7 +904,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
),
],
leading: Padding(
padding: const EdgeInsets.only(top: 7.0, left: 18),
padding: const EdgeInsets.only(top: 7.0, left: 4),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
@ -1003,6 +1079,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
'NotoKufi',
), ),
),
trailing: _buildNotificationBadge(count: notificationCount),
onTap: () => _navigateTo(context, '/notification'),
),
ListTile(