chore: Flutter mobile app, CI, and dev tooling
- mobile/: Flutter/Dart merchant mobile app skeleton - .github/: GitHub Actions CI workflows - .dockerignore: exclude host node_modules from build context - .cursorrules: Cursor IDE project rules - .claude/: Claude Code project settings and launch config Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../core/sync/sync_engine.dart';
|
||||
import '../../core/utils/currency_utils.dart';
|
||||
import 'cart_state.dart';
|
||||
|
||||
class CartScreen extends ConsumerStatefulWidget {
|
||||
const CartScreen({super.key, required this.slug});
|
||||
|
||||
final String slug;
|
||||
|
||||
@override
|
||||
ConsumerState<CartScreen> createState() => _CartScreenState();
|
||||
}
|
||||
|
||||
class _CartScreenState extends ConsumerState<CartScreen> {
|
||||
final _phoneController = TextEditingController();
|
||||
final _nameController = TextEditingController();
|
||||
bool _submitting = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_phoneController.dispose();
|
||||
_nameController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _checkout() async {
|
||||
final cart = ref.read(cartProvider);
|
||||
if (cart.lines.isEmpty) return;
|
||||
|
||||
setState(() => _submitting = true);
|
||||
final api = ref.read(publicApiProvider);
|
||||
final sync = SyncEngine();
|
||||
|
||||
try {
|
||||
final result = await api.placeOrder(
|
||||
widget.slug,
|
||||
tableId: cart.tableId,
|
||||
items: cart.lines.map((l) => l.toOrderJson()).toList(),
|
||||
guestPhone: _phoneController.text.isEmpty ? null : _phoneController.text,
|
||||
guestName: _nameController.text.isEmpty ? null : _nameController.text,
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (result != null) {
|
||||
ref.read(cartProvider.notifier).clear();
|
||||
final orderId = result['orderId'] as String;
|
||||
context.go('/order/$orderId/track');
|
||||
}
|
||||
} catch (_) {
|
||||
sync.enqueueAttendance(
|
||||
action: 'guest-order',
|
||||
cafeId: widget.slug,
|
||||
employeeId: cart.tableId ?? '',
|
||||
);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('آفلاین ذخیره شد — پس از اتصال دوباره تلاش کنید')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _submitting = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cart = ref.watch(cartProvider);
|
||||
|
||||
return Directionality(
|
||||
textDirection: TextDirection.rtl,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(title: const Text('سبد خرید')),
|
||||
body: cart.lines.isEmpty
|
||||
? const Center(child: Text('سبد خالی است'))
|
||||
: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
Text(cart.cafeName ?? '', style: Theme.of(context).textTheme.titleLarge),
|
||||
if (cart.tableNumber != null)
|
||||
Text('میز ${cart.tableNumber}'),
|
||||
const SizedBox(height: 16),
|
||||
...cart.lines.map(
|
||||
(line) => ListTile(
|
||||
title: Text('${line.name} × ${line.quantity}'),
|
||||
subtitle: Text(formatToman(line.lineTotal)),
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
onPressed: () => ref.read(cartProvider.notifier).removeItem(line.menuItemId),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
Text('جمع: ${formatToman(cart.subtotal)}', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(labelText: 'نام (اختیاری)'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _phoneController,
|
||||
decoration: const InputDecoration(labelText: 'موبایل (اختیاری)'),
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton(
|
||||
onPressed: _submitting ? null : _checkout,
|
||||
child: _submitting
|
||||
? const SizedBox(
|
||||
height: 20,
|
||||
width: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('ثبت سفارش'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/api/api_client.dart';
|
||||
import '../public/public_api.dart';
|
||||
|
||||
class CartLine {
|
||||
CartLine({
|
||||
required this.menuItemId,
|
||||
required this.name,
|
||||
required this.unitPrice,
|
||||
this.quantity = 1,
|
||||
this.notes,
|
||||
});
|
||||
|
||||
final String menuItemId;
|
||||
final String name;
|
||||
final int unitPrice;
|
||||
int quantity;
|
||||
final String? notes;
|
||||
|
||||
int get lineTotal => unitPrice * quantity;
|
||||
|
||||
Map<String, dynamic> toOrderJson() => {
|
||||
'menuItemId': menuItemId,
|
||||
'quantity': quantity,
|
||||
if (notes != null && notes!.isNotEmpty) 'notes': notes,
|
||||
};
|
||||
}
|
||||
|
||||
class CartState {
|
||||
CartState({
|
||||
this.cafeSlug,
|
||||
this.cafeName,
|
||||
this.tableId,
|
||||
this.tableNumber,
|
||||
this.lines = const [],
|
||||
});
|
||||
|
||||
final String? cafeSlug;
|
||||
final String? cafeName;
|
||||
final String? tableId;
|
||||
final int? tableNumber;
|
||||
final List<CartLine> lines;
|
||||
|
||||
int get itemCount => lines.fold(0, (sum, l) => sum + l.quantity);
|
||||
int get subtotal => lines.fold(0, (sum, l) => sum + l.lineTotal);
|
||||
|
||||
CartState copyWith({
|
||||
String? cafeSlug,
|
||||
String? cafeName,
|
||||
String? tableId,
|
||||
int? tableNumber,
|
||||
List<CartLine>? lines,
|
||||
}) =>
|
||||
CartState(
|
||||
cafeSlug: cafeSlug ?? this.cafeSlug,
|
||||
cafeName: cafeName ?? this.cafeName,
|
||||
tableId: tableId ?? this.tableId,
|
||||
tableNumber: tableNumber ?? this.tableNumber,
|
||||
lines: lines ?? this.lines,
|
||||
);
|
||||
}
|
||||
|
||||
class CartNotifier extends StateNotifier<CartState> {
|
||||
CartNotifier() : super(CartState());
|
||||
|
||||
void setContext({
|
||||
required String slug,
|
||||
required String cafeName,
|
||||
String? tableId,
|
||||
int? tableNumber,
|
||||
}) {
|
||||
state = CartState(
|
||||
cafeSlug: slug,
|
||||
cafeName: cafeName,
|
||||
tableId: tableId,
|
||||
tableNumber: tableNumber,
|
||||
lines: state.lines,
|
||||
);
|
||||
}
|
||||
|
||||
void addItem(CartLine line) {
|
||||
final existing = state.lines.where((l) => l.menuItemId == line.menuItemId).toList();
|
||||
if (existing.isNotEmpty) {
|
||||
existing.first.quantity += line.quantity;
|
||||
state = state.copyWith(lines: [...state.lines]);
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(lines: [...state.lines, line]);
|
||||
}
|
||||
|
||||
void removeItem(String menuItemId) {
|
||||
state = state.copyWith(
|
||||
lines: state.lines.where((l) => l.menuItemId != menuItemId).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
void clear() => state = CartState();
|
||||
}
|
||||
|
||||
final apiClientProvider = Provider<ApiClient>((ref) => ApiClient());
|
||||
|
||||
final publicApiProvider = Provider<PublicApi>((ref) => PublicApi(ref.watch(apiClientProvider)));
|
||||
|
||||
final cartProvider = StateNotifierProvider<CartNotifier, CartState>((ref) => CartNotifier());
|
||||
Reference in New Issue
Block a user