odoo_loyalty_app/lib/screens/notifications_screen.dart
2026-03-21 15:26:51 +07:00

80 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import '../services/odoo_service.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('Promo Notifications')),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: _notifications.isEmpty
? const Center(child: Text('No new notifications.', style: TextStyle(fontSize: 16)))
: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _notifications.length,
itemBuilder: (context, index) {
final notif = _notifications[index];
return Card(
child: ListTile(
leading: const Icon(Icons.campaign, color: Colors.amber, size: 36),
title: Text(notif['title'] ?? 'Notice', style: const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(notif['body'] ?? ''),
),
),
);
},
),
);
}
}