odoo_loyalty_app/lib/screens/loyalty_dashboard.dart

263 lines
8.7 KiB
Dart

import 'package:flutter/material.dart';
import '../services/odoo_service.dart';
import '../widgets/carousel_widget.dart';
import '../widgets/promo_card_widget.dart';
import '../widgets/subscription_list_widget.dart';
/// Home tab — shows loyalty card, subscriptions, carousel, and promo highlights.
/// Notification polling and AppBar are handled by MainShell.
class LoyaltyDashboard extends StatefulWidget {
final int partnerId;
const LoyaltyDashboard({super.key, required this.partnerId});
@override
State<LoyaltyDashboard> createState() => _LoyaltyDashboardState();
}
class _LoyaltyDashboardState extends State<LoyaltyDashboard> {
List<dynamic> _loyaltyCards = [];
List<dynamic> _subscriptions = [];
List<dynamic> _carouselSlides = [];
List<dynamic> _promos = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_fetchAll();
}
Future<void> _fetchAll() async {
setState(() => _isLoading = true);
try {
final results = await Future.wait([
OdooService().getLoyaltyCards(widget.partnerId),
OdooService().getSubscriptionCards(widget.partnerId),
OdooService().getCmsContent(),
]);
final rawCards = results[0] as List<dynamic>;
final rawSubs = results[1] as List<dynamic>;
final cms = results[2] as Map<String, dynamic>;
if (mounted) {
setState(() {
_loyaltyCards = rawCards;
_subscriptions = rawSubs;
_carouselSlides = (cms['carousel'] as List<dynamic>?) ?? [];
_promos = (cms['promos'] as List<dynamic>?) ?? [];
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
setState(() => _isLoading = false);
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error loading data: $e')));
}
}
}
@override
Widget build(BuildContext context) {
if (_isLoading) {
return const Center(child: CircularProgressIndicator());
}
return RefreshIndicator(
onRefresh: _fetchAll,
child: ListView(
children: [
// ── Loyalty Card ─────────────────────────────────────────────
if (_loyaltyCards.isEmpty)
Padding(
padding: const EdgeInsets.fromLTRB(24, 32, 24, 0),
child: Text(
'No active loyalty card yet.',
style: Theme.of(context).textTheme.titleLarge,
textAlign: TextAlign.center,
),
)
else
..._loyaltyCards.map((card) => _LoyaltyCardTile(card: card)),
// ── Subscriptions ─────────────────────────────────────────────
if (_subscriptions.isNotEmpty)
SubscriptionListWidget(subscriptions: _subscriptions),
const SizedBox(height: 20),
// ── Carousel ──────────────────────────────────────────────────
if (_carouselSlides.isNotEmpty) ...[
CarouselWidget(slides: _carouselSlides),
const SizedBox(height: 24),
],
// ── Promo Highlights ─────────────────────────────────────────
if (_promos.isNotEmpty) ...[
PromoCardRow(promos: _promos),
const SizedBox(height: 24),
],
],
),
);
}
}
class _LoyaltyCardTile extends StatelessWidget {
final dynamic card;
const _LoyaltyCardTile({required this.card});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
final programName = (card['program_id']?[1] as String? ?? '').toLowerCase();
String tier = 'MEMBER';
if (programName.contains('silver')) tier = 'SILVER MEMBER';
if (programName.contains('gold')) tier = 'GOLD MEMBER';
if (programName.contains('platinum')) tier = 'PLATINUM MEMBER';
final onPrimary = colorScheme.onPrimary;
final accentColor = colorScheme.secondary;
return Container(
margin: const EdgeInsets.fromLTRB(16, 16, 16, 8),
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: colorScheme.primary,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: accentColor.withValues(alpha: 0.5),
width: 1.5,
),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.12),
blurRadius: 16,
offset: const Offset(0, 6),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
'${card['program_id']?[1] ?? 'Loyalty Program'}',
style: theme.textTheme.titleLarge?.copyWith(
color: onPrimary,
fontFamily: 'serif',
),
softWrap: true,
),
),
const SizedBox(width: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: accentColor,
borderRadius: BorderRadius.circular(20),
),
child: Text(
tier,
style: theme.textTheme.labelLarge?.copyWith(
color: colorScheme.primary.computeLuminance() > 0.5
? Colors.black
: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 9,
letterSpacing: 0.8,
),
),
),
],
),
const SizedBox(height: 24),
Text(
'MEMBERSHIP CODE',
style: theme.textTheme.bodySmall?.copyWith(
color: onPrimary.withValues(alpha: 0.7),
fontWeight: FontWeight.bold,
fontSize: 10,
letterSpacing: 1.0,
),
),
const SizedBox(height: 4),
Text(
'${card['code'] ?? 'N/A'}',
style: theme.textTheme.titleMedium?.copyWith(
color: onPrimary,
fontFamily: 'monospace',
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'AVAILABLE POINTS',
style: theme.textTheme.bodySmall?.copyWith(
color: onPrimary.withValues(alpha: 0.7),
fontWeight: FontWeight.bold,
fontSize: 10,
letterSpacing: 1.0,
),
),
const SizedBox(height: 4),
Row(
children: [
Icon(
Icons.restaurant_rounded,
color: accentColor,
size: 16,
),
const SizedBox(width: 6),
Text(
'Dine & Save',
style: theme.textTheme.bodyMedium?.copyWith(
color: onPrimary.withValues(alpha: 0.9),
fontStyle: FontStyle.italic,
),
),
],
),
],
),
const SizedBox(width: 16),
Expanded(
child: Align(
alignment: Alignment.bottomRight,
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'${card['points'] ?? 0}',
style: theme.textTheme.displayMedium?.copyWith(
color: accentColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
],
),
);
}
}