Initial commit of Flutter project
This commit is contained in:
@@ -1,41 +1,103 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:glowwheels/constants/api.dart';
|
||||
import 'package:glowwheels/models/order_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../models/order_model.dart';
|
||||
import '../models/serviceboy_model.dart';
|
||||
|
||||
class OrderProvider with ChangeNotifier {
|
||||
final List<Order> _orders = [
|
||||
Order(
|
||||
customerName: "Ankit Ghosh",
|
||||
mobileNumber: "8617015476",
|
||||
serviceType: "Doorstep Service",
|
||||
service: "Foam Wash",
|
||||
price: "₹ 104",
|
||||
time: "10:00 - 11:00 AM",
|
||||
date: "2025-05-28",
|
||||
carName: "Mahindra XUV 700",
|
||||
status: "Confirmed",
|
||||
imagePath: "assets/images/car.jpg",
|
||||
),
|
||||
Order(
|
||||
customerName: "Ravi Kumar",
|
||||
mobileNumber: "9876543210",
|
||||
serviceType: "Workshop",
|
||||
service: "Interior Cleaning",
|
||||
price: "₹ 150",
|
||||
time: "12:00 - 1:00 PM",
|
||||
date: "2025-05-29",
|
||||
carName: "Hyundai Creta",
|
||||
status: "Pending",
|
||||
imagePath: "assets/images/bike.png",
|
||||
),
|
||||
// Add more sample orders if needed
|
||||
];
|
||||
class OrdersProvider with ChangeNotifier {
|
||||
List<Order> _orders = [];
|
||||
bool _isLoading = false;
|
||||
bool _isRefreshing = false;
|
||||
String? _errorMessage;
|
||||
|
||||
List<Order> get orders => _orders;
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isRefreshing => _isRefreshing;
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
void assignServiceBoy(int index, ServiceBoy boy) {
|
||||
_orders[index].assignedBoy = boy;
|
||||
Future<void> fetchOrders(String shopId, {bool refresh = false}) async {
|
||||
final ordersUri = Uri.parse(ApiConstants.ordersByShop(shopId));
|
||||
|
||||
if (refresh) {
|
||||
_isRefreshing = true;
|
||||
} else {
|
||||
_isLoading = true;
|
||||
}
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final response = await http.get(ordersUri);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
|
||||
if (data['success'] == true) {
|
||||
final List<dynamic> ordersJson = data['orders'];
|
||||
_orders = ordersJson.map((orderJson) => Order.fromJson(orderJson)).toList();
|
||||
_errorMessage = null;
|
||||
} else {
|
||||
_errorMessage = 'Failed to fetch orders';
|
||||
}
|
||||
} else {
|
||||
_errorMessage = 'Server error: ${response.statusCode}';
|
||||
}
|
||||
} catch (e) {
|
||||
_errorMessage = 'An error occurred: $e';
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
_isRefreshing = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> assignServiceBoyToOrder({
|
||||
required String orderId,
|
||||
required String serviceBoyId,
|
||||
required String shopId,
|
||||
}) async {
|
||||
final assignUri = Uri.parse(ApiConstants.assignServiceBoy(orderId));
|
||||
|
||||
try {
|
||||
final response = await http.put(
|
||||
assignUri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'serviceBoyId': serviceBoyId}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print('Successfully assigned service boy');
|
||||
} else {
|
||||
print('Failed to assign service boy: ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error assigning service boy: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateOrderStatus({
|
||||
required String orderId,
|
||||
required String status,
|
||||
}) async {
|
||||
final updateStatusUri = Uri.parse(ApiConstants.updateOrderStatusByAdmin(orderId));
|
||||
|
||||
try {
|
||||
final response = await http.put(
|
||||
updateStatusUri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'status': status}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final index = _orders.indexWhere((o) => o.id == orderId);
|
||||
if (index != -1) {
|
||||
_orders[index] = _orders[index].copyWith(status: status);
|
||||
notifyListeners();
|
||||
}
|
||||
} else {
|
||||
print('Failed to update status: ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error updating order status: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user