fixedlocationhomepage

This commit is contained in:
2025-02-22 12:35:25 +05:30
parent e3b601ee7d
commit 2c3e7e2992
16 changed files with 244 additions and 116 deletions

View File

@@ -1,5 +1,9 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:grocery_app/src/core/network_services/service_locator.dart';
import 'package:grocery_app/src/core/routes/routes.dart';
import 'package:grocery_app/src/data/allProduct_model.dart';
@@ -13,6 +17,10 @@ import 'package:grocery_app/src/logic/repo/product_repo.dart';
import 'package:grocery_app/utils/constants/shared_pref_utils.dart';
import 'package:grocery_app/utils/extensions/extensions.dart';
import 'package:http/http.dart' as http;
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:provider/provider.dart';
class ProductProvider extends ChangeNotifier {
final _homeRepo = getIt<ProductRepo>();
@@ -95,21 +103,24 @@ class ProductProvider extends ChangeNotifier {
List<Product> homeproducts = [];
Future<void> getHomeProduct(BuildContext context,String id, String search) async
{
Future<void> getHomeProduct(BuildContext context, String id, String search,
String minPrice, String maxprice, orderby) async {
isHomeLoadingg = true;
notifyListeners();
var data = {"minPrice": "", "minPrice": "", "search": search};
var result = await _homeRepo.getAllProduct(data, context,id);
var data = {
"minPrice": "${minPrice}",
"minPrice": "${maxprice}",
"search": search,
"sortBy": orderby
};
var result = await _homeRepo.getAllProduct(data, context, id);
return result.fold(
(error) {
isLoadingg = false;
notifyListeners();
},
(response)
{
(response) {
homeproducts = response.data!;
isHomeLoadingg = false;
@@ -147,11 +158,10 @@ class ProductProvider extends ChangeNotifier {
bool isBestdealingloading = true;
Future<void> getBestDealProduct(BuildContext context,String search) async
{
Future<void> getBestDealProduct(BuildContext context, String search) async {
isBestdealingloading = true;
notifyListeners();
var data = {"minPrice": "", "minPrice": "", "search": search};
var data = {"minPrice": "", "minPrice": "", "search": search};
var result = await _homeRepo.getBestDealProduct(data, context);
return result.fold(
(error) {
@@ -166,9 +176,6 @@ class ProductProvider extends ChangeNotifier {
);
}
List<Datum> categoryList = [];
bool iscategroyloading = true;
@@ -675,4 +682,83 @@ class ProductProvider extends ChangeNotifier {
_selectedIndex = index;
notifyListeners();
}
Future<void> determinePosition() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return;
}
LocationPermission permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return;
}
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
LatLng currentLatLng = LatLng(position.latitude, position.longitude);
_getAddressFromLatLng(position.latitude, position.longitude);
}
String googleApiKey = "AIzaSyAi3_Dls63iGs7Nccgdm-4FkS0rhT03-4U";
String getCurrentAdd = '';
Future<void> _getAddressFromLatLng(double lat, double lng) async {
final String url =
"https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&key=$googleApiKey";
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data["status"] == "OK") {
var result = data["results"][0]; // First result is most accurate
// _RoadController.text = result["formatted_address"];
List components = result["address_components"];
String roadName = "";
String colony = "";
String buildingName = "";
String pincode = "";
for (var component in components) {
List types = component["types"];
if (types.contains("route")) {
roadName = component["long_name"]; // Road Name
} else if (types.contains("sublocality_level_1") ||
types.contains("locality")) {
colony = component["long_name"]; // Colony Name
} else if (types.contains("premise") ||
types.contains("street_number")) {
buildingName = component["long_name"]; // Building Name
} else if (types.contains("postal_code")) {
pincode = component["long_name"]; // Extract Pin Code
}
}
// setState(() {
// // _address = formattedAddress;
// _roadName = roadName;
// _colony = colony;
// _buildingName = buildingName;
// });
getCurrentAdd = result["formatted_address"] + pincode;
notifyListeners();
print(
"Full Address: ${result["formatted_address"]} ${response.body} sdfsgd ${pincode}");
print("Road Name: $roadName");
print("Colony: $colony");
print("Building Name: $buildingName");
} else {}
} else {}
} catch (e) {
print("Error fetching address: $e");
}
}
}