addtocart
This commit is contained in:
126
lib/src/logic/provider/addTocart_provider.dart
Normal file
126
lib/src/logic/provider/addTocart_provider.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:grocery_app/src/core/network_services/service_locator.dart';
|
||||
import 'package:grocery_app/src/data/all_cart_items.dart';
|
||||
import 'package:grocery_app/src/logic/repo/product_repo.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class AddtocartProvider extends ChangeNotifier {
|
||||
String _pinCode = "Fetching...";
|
||||
bool _isLoading = false;
|
||||
bool _isDeliverable = false;
|
||||
|
||||
String get pinCode => _pinCode;
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isDeliverable => _isDeliverable;
|
||||
|
||||
Future<void> getCurrentLocation(BuildContext context) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
_pinCode = "Location services disabled.";
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LocationPermission permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
_pinCode = "Permission denied.";
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Position position = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.high,
|
||||
);
|
||||
|
||||
print("Location fetched: ${position.latitude}, ${position.longitude}");
|
||||
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(
|
||||
position.latitude,
|
||||
position.longitude,
|
||||
);
|
||||
|
||||
if (placemarks.isNotEmpty) {
|
||||
_pinCode = placemarks.first.postalCode ?? "Unknown";
|
||||
print("Pincode found: $_pinCode");
|
||||
|
||||
// Now check if this pin code is deliverable
|
||||
await checkPin(context, _pinCode);
|
||||
} else {
|
||||
_pinCode = "Could not fetch pin code.";
|
||||
print("Error: No placemarks found.");
|
||||
}
|
||||
} catch (e) {
|
||||
_pinCode = "Error: ${e.toString()}";
|
||||
print("Error: ${e.toString()}");
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> checkPin(BuildContext context, pin) async {
|
||||
var data = {};
|
||||
try {
|
||||
var result = await _homeRepo.checkPin(data, pin);
|
||||
|
||||
return result.fold(
|
||||
(error) {
|
||||
_isDeliverable = false;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
(response) {
|
||||
print("kdhfgkjfkghkfghkj ${response.isDeliverable!}");
|
||||
if (response.isDeliverable!) {
|
||||
_isDeliverable = true;
|
||||
}
|
||||
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_isDeliverable = false;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
final _homeRepo = getIt<ProductRepo>();
|
||||
|
||||
AllCartItems allitem = AllCartItems();
|
||||
|
||||
bool isLoaddcartItem = true;
|
||||
Future<void> getItemCards(BuildContext context) async {
|
||||
var data = {};
|
||||
try {
|
||||
var result = await _homeRepo.getItemCards(data);
|
||||
|
||||
return result.fold(
|
||||
(error) {
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
(response) {
|
||||
allitem = response!;
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
isLoaddcartItem = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user