Initial commit
This commit is contained in:
91
lib/providers/order_provider.dart
Normal file
91
lib/providers/order_provider.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../models/order.dart';
|
||||
import '../services/api_service.dart';
|
||||
|
||||
class OrderState {
|
||||
final List<OrderItem> 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<OrderItem>? 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<OrderState> {
|
||||
OrderNotifier() : super(const OrderState());
|
||||
|
||||
Future<void> 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<void> 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<void> 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<void> 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<OrderNotifier, OrderState>((ref) {
|
||||
return OrderNotifier();
|
||||
});
|
||||
Reference in New Issue
Block a user