get events from current position
This commit is contained in:
parent
5c0f4d345d
commit
b22458021b
@ -11,6 +11,7 @@ import 'pages/ListItemMenu.dart';
|
|||||||
import 'classes/alert.dart';
|
import 'classes/alert.dart';
|
||||||
|
|
||||||
import 'variable/globals.dart' as globals;
|
import 'variable/globals.dart' as globals;
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(MyApp());
|
runApp(MyApp());
|
||||||
@ -153,10 +154,49 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
_checkLocationPermission();
|
||||||
start();
|
start();
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _checkLocationPermission() async {
|
||||||
|
PermissionStatus status = await Permission.location.status;
|
||||||
|
|
||||||
|
if (status.isGranted) {
|
||||||
|
print("Location permission granted");
|
||||||
|
} else if (status.isDenied) {
|
||||||
|
print("Location permission denied");
|
||||||
|
_requestLocationPermission();
|
||||||
|
} else if (status.isPermanentlyDenied) {
|
||||||
|
print("Location permission permanently denied");
|
||||||
|
openAppSettings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request location permission
|
||||||
|
Future<void> _requestLocationPermission() async {
|
||||||
|
PermissionStatus status = await Permission.location.request();
|
||||||
|
|
||||||
|
if (status.isGranted) {
|
||||||
|
print("Location permission granted");
|
||||||
|
} else if (status.isDenied) {
|
||||||
|
print("Location permission denied");
|
||||||
|
} else if (status.isPermanentlyDenied) {
|
||||||
|
print("Location permission permanently denied");
|
||||||
|
openAppSettings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open app settings to allow user to grant permission manually
|
||||||
|
Future<void> _openAppSettings() async {
|
||||||
|
bool opened = await openAppSettings();
|
||||||
|
if (opened) {
|
||||||
|
print("App settings opened");
|
||||||
|
} else {
|
||||||
|
print("Failed to open app settings");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
@ -47,6 +47,52 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
|
|
||||||
// Fetching events from API
|
// Fetching events from API
|
||||||
static Future<List<Events>> getPosts() async {
|
static Future<List<Events>> getPosts() async {
|
||||||
|
PermissionStatus status = await Permission.location.status;
|
||||||
|
var url = Uri.parse("${globals.api}/events");
|
||||||
|
if (status.isGranted) {
|
||||||
|
print("Location permission granted");
|
||||||
|
|
||||||
|
// Get the current position with high accuracy
|
||||||
|
LocationSettings locationSettings = LocationSettings(
|
||||||
|
accuracy: LocationAccuracy.high,
|
||||||
|
distanceFilter:
|
||||||
|
10, // Optional: Minimum distance (in meters) to trigger location update
|
||||||
|
);
|
||||||
|
|
||||||
|
Position position = await Geolocator.getCurrentPosition(
|
||||||
|
locationSettings: locationSettings,
|
||||||
|
);
|
||||||
|
// Calculate the boundaries
|
||||||
|
double radiusInKm = 50;
|
||||||
|
double latDistance = radiusInKm / 111.0;
|
||||||
|
double lonDistance =
|
||||||
|
radiusInKm / (111.0 * cos(position.latitude * pi / 180));
|
||||||
|
|
||||||
|
double minLat = position.latitude - latDistance;
|
||||||
|
double maxLat = position.latitude + latDistance;
|
||||||
|
double minLon = position.longitude - lonDistance;
|
||||||
|
double maxLon = position.longitude + lonDistance;
|
||||||
|
|
||||||
|
url = Uri.parse("${globals.api}/events/search"
|
||||||
|
"?min_lat=$minLat&max_lat=$maxLat"
|
||||||
|
"&min_lon=$minLon&max_lon=$maxLon");
|
||||||
|
}
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
|
final List<Events> body = [];
|
||||||
|
if (accessToken.isNotEmpty) {
|
||||||
|
final response = await http.get(url, headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||||
|
});
|
||||||
|
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||||
|
return body.map((e) => Events.fromJson(e)).toList();
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetching events from API
|
||||||
|
Future<List<Events>> getAllPosts() async {
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
var accessToken = prefs.getString("access_token") ?? "";
|
var accessToken = prefs.getString("access_token") ?? "";
|
||||||
final List<Events> body = [];
|
final List<Events> body = [];
|
||||||
@ -65,54 +111,17 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_checkLocationPermission();
|
|
||||||
// Initialize data fetch when the page loads
|
// Initialize data fetch when the page loads
|
||||||
_getCurrentLocation();
|
_getCurrentLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if location permission is granted
|
// Get the device's current location
|
||||||
Future<void> _checkLocationPermission() async {
|
Future<void> _getCurrentLocation() async {
|
||||||
PermissionStatus status = await Permission.location.status;
|
PermissionStatus status = await Permission.location.status;
|
||||||
|
|
||||||
if (status.isGranted) {
|
if (status.isGranted) {
|
||||||
print("Location permission granted");
|
print("Location permission granted");
|
||||||
} else if (status.isDenied) {
|
|
||||||
print("Location permission denied");
|
|
||||||
_requestLocationPermission();
|
|
||||||
} else if (status.isPermanentlyDenied) {
|
|
||||||
print("Location permission permanently denied");
|
|
||||||
openAppSettings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request location permission
|
|
||||||
Future<void> _requestLocationPermission() async {
|
|
||||||
PermissionStatus status = await Permission.location.request();
|
|
||||||
|
|
||||||
if (status.isGranted) {
|
|
||||||
print("Location permission granted");
|
|
||||||
} else if (status.isDenied) {
|
|
||||||
print("Location permission denied");
|
|
||||||
_fetchInitialData();
|
|
||||||
} else if (status.isPermanentlyDenied) {
|
|
||||||
print("Location permission permanently denied");
|
|
||||||
openAppSettings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open app settings to allow user to grant permission manually
|
|
||||||
Future<void> _openAppSettings() async {
|
|
||||||
bool opened = await openAppSettings();
|
|
||||||
if (opened) {
|
|
||||||
print("App settings opened");
|
|
||||||
} else {
|
|
||||||
print("Failed to open app settings");
|
|
||||||
_fetchInitialData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the device's current location
|
|
||||||
Future<void> _getCurrentLocation() async {
|
|
||||||
// Get the current position with high accuracy
|
// Get the current position with high accuracy
|
||||||
LocationSettings locationSettings = LocationSettings(
|
LocationSettings locationSettings = LocationSettings(
|
||||||
accuracy: LocationAccuracy.high,
|
accuracy: LocationAccuracy.high,
|
||||||
@ -128,6 +137,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
final place =
|
final place =
|
||||||
await _getCityAndCountry(position.latitude, position.longitude);
|
await _getCityAndCountry(position.latitude, position.longitude);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Method to get city and country from latitude and longitude using Mapbox API
|
// Method to get city and country from latitude and longitude using Mapbox API
|
||||||
Future<void> _getCityAndCountry(double latitude, double longitude) async {
|
Future<void> _getCityAndCountry(double latitude, double longitude) async {
|
||||||
@ -156,10 +166,10 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
String country = _getCountryFromFeatures(features);
|
String country = _getCountryFromFeatures(features);
|
||||||
print("city : ${city} ${country}");
|
print("city : ${city} ${country}");
|
||||||
if (city.isNotEmpty && country.isNotEmpty) {
|
if (city.isNotEmpty && country.isNotEmpty) {
|
||||||
|
fetchPostsByLocation(latitude, longitude);
|
||||||
setState(() {
|
setState(() {
|
||||||
inputGeo.text = "${city}, ${country}";
|
inputGeo.text = "${city}, ${country}";
|
||||||
});
|
});
|
||||||
fetchPostsByLocation(latitude, longitude);
|
|
||||||
} else {
|
} else {
|
||||||
_fetchInitialData();
|
_fetchInitialData();
|
||||||
}
|
}
|
||||||
@ -171,8 +181,8 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
throw Exception('Failed to load location data');
|
throw Exception('Failed to load location data');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error getting city and country: $e");
|
|
||||||
_fetchInitialData();
|
_fetchInitialData();
|
||||||
|
print("Error getting city and country: $e");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +212,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
Future<void> _fetchInitialData() async {
|
Future<void> _fetchInitialData() async {
|
||||||
try {
|
try {
|
||||||
// Optionally, you can fetch posts initially if needed.
|
// Optionally, you can fetch posts initially if needed.
|
||||||
List<Events> initialPosts = await getPosts();
|
List<Events> initialPosts = await getAllPosts();
|
||||||
setState(() {
|
setState(() {
|
||||||
// Assign to the postsFuture and update the filtered posts if needed
|
// Assign to the postsFuture and update the filtered posts if needed
|
||||||
filteredPosts = initialPosts;
|
filteredPosts = initialPosts;
|
||||||
@ -392,6 +402,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
|
|
||||||
// Function to display fetched data on screen
|
// Function to display fetched data on screen
|
||||||
Widget buildPosts(List<Events> posts) {
|
Widget buildPosts(List<Events> posts) {
|
||||||
|
print("posts : ${posts}");
|
||||||
print("filteredposts : ${filteredPosts}");
|
print("filteredposts : ${filteredPosts}");
|
||||||
final displayedPosts = filteredPosts;
|
final displayedPosts = filteredPosts;
|
||||||
print("results ${displayedPosts}");
|
print("results ${displayedPosts}");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user