odoo_loyalty_app/lib/screens/notifications_screen.dart

106 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:odoo_rpc/odoo_rpc.dart';
import '../services/odoo_service.dart';
import '../theme/app_theme.dart';
class NotificationsScreen extends StatefulWidget {
const NotificationsScreen({super.key});
@override
State<NotificationsScreen> createState() => _NotificationsScreenState();
}
class _NotificationsScreenState extends State<NotificationsScreen> {
List<dynamic> _notifications = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_fetchNotifications();
}
Future<void> _fetchNotifications() async {
try {
final client = OdooService().client;
if (client == null) throw Exception("Not connected");
final response = await client.callRPC(
'/api/loyalty/fetch_notifications',
'call',
{'last_id': 0}
);
if (response != null && response['status'] == 'success') {
if (mounted) {
setState(() {
_notifications = response['data'];
_isLoading = false;
});
}
} else {
throw Exception("Invalid response from server");
}
} catch (e) {
if (mounted) {
setState(() => _isLoading = false);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error loading notifications: $e')));
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Notifications')),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: _notifications.isEmpty
? const Center(child: Text('No new promos.', style: TextStyle(fontSize: 16)))
: ListView.builder(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
itemCount: _notifications.length,
itemBuilder: (context, index) {
final notif = _notifications[index];
return Container(
margin: const EdgeInsets.only(bottom: 16),
decoration: BoxDecoration(
color: AppTheme.surfaceContainerLow,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: AppTheme.onSurface.withOpacity(0.04),
blurRadius: 16,
offset: const Offset(0, 4),
)
]
),
child: ListTile(
contentPadding: const EdgeInsets.all(16),
leading: Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: AppTheme.primaryContainer.withOpacity(0.1),
shape: BoxShape.circle,
),
child: const Icon(Icons.star, color: AppTheme.primary),
),
title: Text(
notif['title'] ?? 'Notice',
style: Theme.of(context).textTheme.titleMedium,
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
notif['body'] ?? '',
style: Theme.of(context).textTheme.bodyMedium,
),
),
),
);
},
),
);
}
}