odoo_loyalty_app/lib/screens/orders_screen.dart

198 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import '../services/odoo_service.dart';
import '../theme/app_theme.dart';
class OrdersScreen extends StatefulWidget {
const OrdersScreen({super.key});
@override
State<OrdersScreen> createState() => _OrdersScreenState();
}
class _OrdersScreenState extends State<OrdersScreen> {
List<dynamic> _history = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_fetchHistory();
}
Future<void> _fetchHistory() async {
if (mounted) setState(() => _isLoading = true);
try {
final history = await OdooService().getOrderHistory();
if (mounted) {
setState(() {
_history = history;
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() => _isLoading = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error loading history: $e')),
);
}
}
}
String _formatDate(String rawDate) {
if (rawDate.isEmpty) return '';
try {
final parsed = DateTime.parse(rawDate);
final months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];
final year = parsed.year;
final month = months[parsed.month - 1];
final day = parsed.day.toString().padLeft(2, '0');
final hour = parsed.hour.toString().padLeft(2, '0');
final minute = parsed.minute.toString().padLeft(2, '0');
return '$day $month $year, $hour:$minute';
} catch (_) {
return rawDate;
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (_isLoading) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return Scaffold(
appBar: AppBar(
title: const Text('Order & Points History'),
),
body: _history.isEmpty
? Center(
child: Padding(
padding: const EdgeInsets.all(40),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.all(28),
decoration: const BoxDecoration(
color: AppTheme.surfaceContainerLow,
shape: BoxShape.circle,
),
child: const Icon(
Icons.receipt_long_rounded,
size: 56,
color: AppTheme.secondary,
),
),
const SizedBox(height: 28),
Text(
'No Orders Yet',
style: theme.textTheme.headlineMedium,
),
const SizedBox(height: 12),
Text(
'Your order history and points transactions will show up here after you make purchases.',
style: theme.textTheme.bodyLarge?.copyWith(
color: AppTheme.onSurfaceVariant,
height: 1.6,
),
textAlign: TextAlign.center,
),
],
),
),
)
: RefreshIndicator(
onRefresh: _fetchHistory,
child: ListView.builder(
padding: const EdgeInsets.symmetric(vertical: 16),
itemCount: _history.length,
itemBuilder: (context, index) {
final item = _history[index];
final isEarn = item['type'] == 'earn';
final orderRef = item['order_ref'] as String? ?? '';
final rawDate = item['date'] as String? ?? '';
final posName = item['pos_name'] as String? ?? '';
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
color: AppTheme.surfaceContainerLow,
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
leading: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: (isEarn ? const Color(0xFF2E7D32) : const Color(0xFFC62828)).withValues(alpha: 0.1),
shape: BoxShape.circle,
),
child: Icon(
isEarn ? Icons.add_card_rounded : Icons.payment_rounded,
color: isEarn ? const Color(0xFF2E7D32) : const Color(0xFFC62828),
size: 24,
),
),
title: Text(
orderRef.isNotEmpty ? orderRef : 'Point Adjustment',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: AppTheme.onSurface,
),
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 6.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (posName.isNotEmpty) ...[
Row(
children: [
Icon(
Icons.storefront_rounded,
size: 14,
color: theme.colorScheme.primary,
),
const SizedBox(width: 4),
Text(
posName,
style: theme.textTheme.bodySmall?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.primary,
),
),
],
),
const SizedBox(height: 4),
],
Text(
_formatDate(rawDate),
style: theme.textTheme.bodySmall?.copyWith(
color: AppTheme.onSurfaceVariant,
),
),
],
),
),
trailing: Text(
'${isEarn ? '+' : ''}${item['points']} pts',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: isEarn ? const Color(0xFF2E7D32) : const Color(0xFFC62828),
fontSize: 16,
),
),
),
);
},
),
),
);
}
}