import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../models/order.dart'; import '../services/api_service.dart'; class OrderState { final List orders; final OrderItem? currentOrder; final bool isLoading; final String? error; final bool verifySuccess; const OrderState({ this.orders = const [], this.currentOrder, this.isLoading = false, this.error, this.verifySuccess = false, }); OrderState copyWith({ List? orders, OrderItem? currentOrder, bool? isLoading, String? error, bool? verifySuccess, }) { return OrderState( orders: orders ?? this.orders, currentOrder: currentOrder ?? this.currentOrder, isLoading: isLoading ?? this.isLoading, error: error, verifySuccess: verifySuccess ?? this.verifySuccess, ); } } class OrderNotifier extends StateNotifier { OrderNotifier() : super(const OrderState()); Future loadOrders(OrderStatus status) async { state = state.copyWith(isLoading: true, error: null); try { final orders = await apiService.getOrders(status); state = state.copyWith(orders: orders, isLoading: false); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString()); } } Future getDetail(String id) async { state = state.copyWith(isLoading: true, error: null); try { final order = await apiService.getOrderDetail(id); state = state.copyWith(currentOrder: order, isLoading: false); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString()); } } Future verifyOrder(String id) async { state = state.copyWith(isLoading: true, error: null, verifySuccess: false); try { final order = await apiService.verifyOrder(id); state = state.copyWith( currentOrder: order, isLoading: false, verifySuccess: true, ); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString()); } } Future scanVerify(String code) async { state = state.copyWith(isLoading: true, error: null); try { final order = await apiService.scanVerify(code); state = state.copyWith(currentOrder: order, isLoading: false); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString()); } } void clearVerifySuccess() { state = state.copyWith(verifySuccess: false); } } final orderProvider = StateNotifierProvider((ref) { return OrderNotifier(); });