notification count added in menu
This commit is contained in:
parent
4c5d95abd9
commit
967fd2fd7a
@ -1,6 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:iconify_design/iconify_design.dart';
|
import 'package:iconify_design/iconify_design.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@ -88,6 +89,10 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
static final _repo = getIt.call<TAuthRepo>();
|
static final _repo = getIt.call<TAuthRepo>();
|
||||||
late final Locale locale;
|
late final Locale locale;
|
||||||
|
|
||||||
|
List<Map<String, dynamic>> notifications = [];
|
||||||
|
List<String> userReadedNotiOrNot = [];
|
||||||
|
int notificationCount = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@ -703,6 +708,12 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
String recordId = userId;
|
String recordId = userId;
|
||||||
String collectionId =
|
String collectionId =
|
||||||
userDetailsResponse.data['collectionId'] ?? '_pb_users_auth_';
|
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) {
|
if (userAvatar!.isNotEmpty && recordId.isNotEmpty) {
|
||||||
_avatarUrl = '$apiUrl/api/files/$collectionId/$recordId/$userAvatar';
|
_avatarUrl = '$apiUrl/api/files/$collectionId/$recordId/$userAvatar';
|
||||||
@ -710,6 +721,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
_avatarUrl = ''; // Reset to default or empty
|
_avatarUrl = ''; // Reset to default or empty
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
fetchNotifications(locale?.languageCode ?? 'en');
|
||||||
// **Set Default Locale in Provider**
|
// **Set Default Locale in Provider**
|
||||||
// if (preferredLanguage == 'ar') {
|
// if (preferredLanguage == 'ar') {
|
||||||
// ref.read(localeProvider.notifier).setLocale(const Locale('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 {
|
// Future<void> logout() async {
|
||||||
// final prefs = await SharedPreferences.getInstance();
|
// final prefs = await SharedPreferences.getInstance();
|
||||||
// // setState(() {
|
// // setState(() {
|
||||||
@ -788,7 +864,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
scrolledUnderElevation: 0,
|
scrolledUnderElevation: 0,
|
||||||
titleSpacing: 0,
|
titleSpacing: 0,
|
||||||
backgroundColor: widget.appbarColor ?? Colors.white,
|
backgroundColor: widget.appbarColor ?? Colors.white,
|
||||||
leadingWidth: widget.showBackButton ? mywidth * 0.20 : null,
|
leadingWidth: widget.showBackButton ? mywidth * 0.18 : null,
|
||||||
bottom: widget.showDivider
|
bottom: widget.showDivider
|
||||||
? PreferredSize(
|
? PreferredSize(
|
||||||
preferredSize: Size.fromHeight(1), // Height of the line
|
preferredSize: Size.fromHeight(1), // Height of the line
|
||||||
@ -828,7 +904,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
leading: Padding(
|
leading: Padding(
|
||||||
padding: const EdgeInsets.only(top: 7.0, left: 18),
|
padding: const EdgeInsets.only(top: 7.0, left: 4),
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
@ -1003,6 +1079,7 @@ class _BaseScaffoldState extends ConsumerState<BaseScaffold> {
|
|||||||
'NotoKufi',
|
'NotoKufi',
|
||||||
), ),
|
), ),
|
||||||
),
|
),
|
||||||
|
trailing: _buildNotificationBadge(count: notificationCount),
|
||||||
onTap: () => _navigateTo(context, '/notification'),
|
onTap: () => _navigateTo(context, '/notification'),
|
||||||
),
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user