payfrit-app/lib/screens/order_detail_screen.dart
John Mizerek ef8421c88a Display cart modifiers with category breadcrumbs
- OrderLineItem now includes itemName, itemParentName, and
  isCheckedByDefault from API response
- Cart view displays modifiers as "Category: Selection" format
  (e.g., "Select Drink: Coke") instead of just item IDs
- No longer requires menu item lookup for modifier names

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-12 19:06:54 -08:00

723 lines
21 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../models/order_detail.dart';
import '../services/api.dart';
class OrderDetailScreen extends StatefulWidget {
final int orderId;
const OrderDetailScreen({super.key, required this.orderId});
@override
State<OrderDetailScreen> createState() => _OrderDetailScreenState();
}
class _OrderDetailScreenState extends State<OrderDetailScreen> {
OrderDetail? _order;
bool _isLoading = true;
String? _error;
@override
void initState() {
super.initState();
_loadOrder();
}
Future<void> _loadOrder() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final order = await Api.getOrderDetail(orderId: widget.orderId);
if (mounted) {
setState(() {
_order = order;
_isLoading = false;
});
}
} catch (e) {
debugPrint('Error loading order detail: $e');
if (mounted) {
setState(() {
_error = e.toString();
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Order #${widget.orderId}'),
),
body: _buildBody(),
);
}
Widget _buildBody() {
if (_isLoading) {
return const Center(child: CircularProgressIndicator());
}
if (_error != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 48,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(height: 16),
Text(
'Failed to load order',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
_error!,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 24),
FilledButton.icon(
onPressed: _loadOrder,
icon: const Icon(Icons.refresh),
label: const Text('Retry'),
),
],
),
),
);
}
final order = _order;
if (order == null) {
return const Center(child: Text('Order not found'));
}
return RefreshIndicator(
onRefresh: _loadOrder,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildOrderHeader(order),
const SizedBox(height: 16),
_buildStatusCard(order),
if (order.staff.isNotEmpty) ...[
const SizedBox(height: 16),
_buildStaffCard(order),
],
const SizedBox(height: 16),
_buildItemsCard(order),
const SizedBox(height: 16),
_buildTotalsCard(order),
if (order.notes.isNotEmpty) ...[
const SizedBox(height: 16),
_buildNotesCard(order),
],
const SizedBox(height: 32),
],
),
),
);
}
Widget _buildOrderHeader(OrderDetail order) {
final dateFormat = DateFormat('MMM d, yyyy');
final timeFormat = DateFormat('h:mm a');
final displayDate = order.submittedOn ?? order.createdOn;
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
order.businessName.isNotEmpty ? order.businessName : 'Order',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
),
_buildOrderTypeChip(order.orderTypeId, order.orderTypeName),
],
),
const SizedBox(height: 8),
Row(
children: [
Icon(
Icons.calendar_today,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 4),
Text(
'${dateFormat.format(displayDate)} at ${timeFormat.format(displayDate)}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
if (order.servicePoint.name.isNotEmpty) ...[
const SizedBox(height: 4),
Row(
children: [
Icon(
Icons.table_restaurant,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 4),
Text(
order.servicePoint.name,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
],
],
),
),
);
}
Widget _buildOrderTypeChip(int typeId, String typeName) {
IconData icon;
Color color;
switch (typeId) {
case 1: // Dine-in
icon = Icons.restaurant;
color = Colors.orange;
break;
case 2: // Takeaway
icon = Icons.shopping_bag;
color = Colors.blue;
break;
case 3: // Delivery
icon = Icons.delivery_dining;
color = Colors.green;
break;
default:
icon = Icons.receipt;
color = Colors.grey;
}
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: color.withOpacity(0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: color.withOpacity(0.3)),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 14, color: color),
const SizedBox(width: 4),
Text(
typeName,
style: TextStyle(
color: color,
fontSize: 12,
fontWeight: FontWeight.w500,
),
),
],
),
);
}
Widget _buildStatusCard(OrderDetail order) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
_buildStatusIcon(order.status),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Status',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
Text(
order.statusText,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
],
),
),
],
),
),
);
}
Widget _buildStatusIcon(int status) {
IconData icon;
Color color;
switch (status) {
case 1: // Submitted
icon = Icons.send;
color = Colors.blue;
break;
case 2: // In Progress
icon = Icons.pending;
color = Colors.orange;
break;
case 3: // Ready
icon = Icons.check_circle_outline;
color = Colors.green;
break;
case 4: // Out for Delivery
icon = Icons.local_shipping;
color = Colors.blue;
break;
case 5: // Delivered
icon = Icons.check_circle;
color = Colors.green;
break;
case 6: // Cancelled
icon = Icons.cancel;
color = Colors.red;
break;
default: // Cart or Unknown
icon = Icons.shopping_cart;
color = Colors.grey;
}
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: color.withOpacity(0.1),
shape: BoxShape.circle,
),
child: Icon(icon, color: color, size: 24),
);
}
Widget _buildStaffCard(OrderDetail order) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.people,
size: 20,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
Text(
'Your Server${order.staff.length > 1 ? 's' : ''}',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
],
),
const SizedBox(height: 16),
Wrap(
spacing: 16,
runSpacing: 16,
children: order.staff.map((staff) => _buildStaffItem(staff)).toList(),
),
],
),
),
);
}
Widget _buildStaffItem(OrderStaff staff) {
return GestureDetector(
onTap: () => _showTipDialog(staff),
child: Column(
children: [
CircleAvatar(
radius: 32,
backgroundColor: Theme.of(context).colorScheme.surfaceContainerHighest,
backgroundImage: staff.avatarUrl.isNotEmpty
? NetworkImage(staff.avatarUrl)
: null,
onBackgroundImageError: staff.avatarUrl.isNotEmpty
? (_, __) {} // Silently handle missing images
: null,
child: staff.avatarUrl.isEmpty
? Icon(
Icons.person,
size: 32,
color: Theme.of(context).colorScheme.onSurfaceVariant,
)
: null,
),
const SizedBox(height: 8),
Text(
staff.firstName.isNotEmpty ? staff.firstName : 'Staff',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 4),
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'Tip',
style: TextStyle(
fontSize: 11,
color: Theme.of(context).colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w500,
),
),
),
],
),
);
}
void _showTipDialog(OrderStaff staff) {
showModalBottomSheet(
context: context,
builder: (context) => Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(
radius: 40,
backgroundColor: Theme.of(context).colorScheme.surfaceContainerHighest,
backgroundImage: staff.avatarUrl.isNotEmpty
? NetworkImage(staff.avatarUrl)
: null,
child: staff.avatarUrl.isEmpty
? Icon(
Icons.person,
size: 40,
color: Theme.of(context).colorScheme.onSurfaceVariant,
)
: null,
),
const SizedBox(height: 12),
Text(
'Tip ${staff.firstName.isNotEmpty ? staff.firstName : "Staff"}',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildTipButton(staff, 2),
_buildTipButton(staff, 5),
_buildTipButton(staff, 10),
],
),
const SizedBox(height: 16),
OutlinedButton(
onPressed: () => _showCustomTipDialog(staff),
child: const Text('Custom Amount'),
),
const SizedBox(height: 16),
],
),
),
);
}
Widget _buildTipButton(OrderStaff staff, int amount) {
return FilledButton(
onPressed: () {
Navigator.pop(context);
_processTip(staff, amount.toDouble());
},
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
child: Text('\$$amount'),
);
}
void _showCustomTipDialog(OrderStaff staff) {
Navigator.pop(context);
final controller = TextEditingController();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Tip ${staff.firstName}'),
content: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(
prefixText: '\$ ',
hintText: '0.00',
border: OutlineInputBorder(),
),
autofocus: true,
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () {
final amount = double.tryParse(controller.text);
if (amount != null && amount > 0) {
Navigator.pop(context);
_processTip(staff, amount);
}
},
child: const Text('Send Tip'),
),
],
),
);
}
void _processTip(OrderStaff staff, double amount) {
// TODO: Implement actual tip processing via API
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Tip of \$${amount.toStringAsFixed(2)} for ${staff.firstName} - Coming soon!',
style: const TextStyle(color: Colors.black),
),
backgroundColor: const Color(0xFF90EE90),
behavior: SnackBarBehavior.floating,
margin: const EdgeInsets.only(bottom: 80, left: 16, right: 16),
),
);
}
Widget _buildItemsCard(OrderDetail order) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Items',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 12),
...order.lineItems.map((item) => _buildLineItem(item)),
],
),
),
);
}
Widget _buildLineItem(OrderLineItemDetail item) {
final nonDefaultMods = item.nonDefaultModifiers;
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Quantity badge
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(4),
),
child: Text(
'${item.quantity}x',
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimaryContainer,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(width: 8),
// Item name
Expanded(
child: Text(
item.itemName,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.w500,
),
),
),
// Price
Text(
'\$${item.totalPrice.toStringAsFixed(2)}',
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.w500,
),
),
],
),
// Non-default modifiers
if (nonDefaultMods.isNotEmpty) ...[
const SizedBox(height: 4),
Padding(
padding: const EdgeInsets.only(left: 36),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: nonDefaultMods.map((mod) {
final modPrice = mod.unitPrice > 0
? ' (+\$${mod.unitPrice.toStringAsFixed(2)})'
: '';
return Padding(
padding: const EdgeInsets.only(top: 2),
child: Text(
'+ ${mod.itemName}$modPrice',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
);
}).toList(),
),
),
],
// Item remarks/notes
if (item.remarks.isNotEmpty) ...[
const SizedBox(height: 4),
Padding(
padding: const EdgeInsets.only(left: 36),
child: Text(
'"${item.remarks}"',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.orange[700],
fontStyle: FontStyle.italic,
),
),
),
],
const Divider(height: 16),
],
),
);
}
Widget _buildTotalsCard(OrderDetail order) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
_buildTotalRow('Subtotal', order.subtotal),
const SizedBox(height: 8),
_buildTotalRow('Tax', order.tax),
if (order.tip > 0) ...[
const SizedBox(height: 8),
_buildTotalRow('Tip', order.tip),
],
const Divider(height: 16),
_buildTotalRow('Total', order.total, isTotal: true),
],
),
),
);
}
Widget _buildTotalRow(String label, double amount, {bool isTotal = false}) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
label,
style: isTotal
? Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
)
: Theme.of(context).textTheme.bodyMedium,
),
Text(
'\$${amount.toStringAsFixed(2)}',
style: isTotal
? Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
)
: Theme.of(context).textTheme.bodyMedium,
),
],
);
}
Widget _buildNotesCard(OrderDetail order) {
return Card(
color: Colors.amber[50],
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(Icons.note, color: Colors.amber[700], size: 20),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Order Notes',
style: TextStyle(
fontSize: 12,
color: Colors.amber[800],
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 4),
Text(
order.notes,
style: TextStyle(
fontSize: 14,
color: Colors.amber[900],
),
),
],
),
),
],
),
),
);
}
}