|
|
|
@@ -1,3 +1,4 @@
|
|
|
|
|
import 'package:covas_mobile/classes/alert.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
|
|
|
|
@@ -51,46 +52,63 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|
|
|
|
// Fetching events from API
|
|
|
|
|
static Future<List<Events>> getPosts() async {
|
|
|
|
|
PermissionStatus status = await Permission.location.status;
|
|
|
|
|
|
|
|
|
|
final List<Events> body = [];
|
|
|
|
|
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,
|
|
|
|
|
const LocationSettings locationSettings = LocationSettings(
|
|
|
|
|
accuracy: LocationAccuracy.medium,
|
|
|
|
|
timeLimit: Duration(seconds: 5),
|
|
|
|
|
);
|
|
|
|
|
// Calculate the boundaries
|
|
|
|
|
double radiusInKm = 50;
|
|
|
|
|
double latDistance = radiusInKm / 111.0;
|
|
|
|
|
double lonDistance =
|
|
|
|
|
radiusInKm / (111.0 * cos(position.latitude * pi / 180));
|
|
|
|
|
Position? position;
|
|
|
|
|
try {
|
|
|
|
|
position = await Geolocator.getCurrentPosition(
|
|
|
|
|
locationSettings: locationSettings);
|
|
|
|
|
} on LocationServiceDisabledException {
|
|
|
|
|
// Handle location services disabled
|
|
|
|
|
print('Location services are disabled.');
|
|
|
|
|
position = await Geolocator.getLastKnownPosition();
|
|
|
|
|
if (position == null) {
|
|
|
|
|
print('No last known position available.');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Handle other errors
|
|
|
|
|
print('Failed to get location: $e');
|
|
|
|
|
position = await Geolocator.getLastKnownPosition();
|
|
|
|
|
if (position == null) {
|
|
|
|
|
print('No last known position available.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (position != null) {
|
|
|
|
|
// 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;
|
|
|
|
|
DateTime currentDatetime = DateTime.now();
|
|
|
|
|
url = Uri.parse("${globals.api}/events/search"
|
|
|
|
|
"?min_lat=$minLat&max_lat=$maxLat"
|
|
|
|
|
"&min_lon=$minLon&max_lon=$maxLon¤t_datetime=${currentDatetime.toString()}");
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
double minLat = position.latitude - latDistance;
|
|
|
|
|
double maxLat = position.latitude + latDistance;
|
|
|
|
|
double minLon = position.longitude - lonDistance;
|
|
|
|
|
double maxLon = position.longitude + lonDistance;
|
|
|
|
|
DateTime currentDatetime = DateTime.now();
|
|
|
|
|
url = Uri.parse("${globals.api}/events/search"
|
|
|
|
|
"?min_lat=$minLat&max_lat=$maxLat"
|
|
|
|
|
"&min_lon=$minLon&max_lon=$maxLon¤t_datetime=${currentDatetime.toString()}");
|
|
|
|
|
}
|
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
|
var accessToken = prefs.getString("access_token") ?? "";
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
@@ -119,8 +137,6 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|
|
|
|
super.initState();
|
|
|
|
|
// Initialize data fetch when the page loads
|
|
|
|
|
_getCurrentLocation();
|
|
|
|
|
|
|
|
|
|
Datepicker.text = DateFormat("dd-MM-yyyy").format(new DateTime.now());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the device's current location
|
|
|
|
@@ -131,19 +147,32 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
const LocationSettings locationSettings = LocationSettings(
|
|
|
|
|
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
|
|
|
|
|
Position? position;
|
|
|
|
|
try {
|
|
|
|
|
position = await Geolocator.getCurrentPosition(
|
|
|
|
|
locationSettings: locationSettings);
|
|
|
|
|
} on LocationServiceDisabledException {
|
|
|
|
|
// Handle location services disabled
|
|
|
|
|
print('Location services are disabled.');
|
|
|
|
|
position = await Geolocator.getLastKnownPosition();
|
|
|
|
|
if (position == null) {
|
|
|
|
|
print('No last known position available.');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Handle other errors
|
|
|
|
|
print('Failed to get location: $e');
|
|
|
|
|
position = await Geolocator.getLastKnownPosition();
|
|
|
|
|
if (position == null) {
|
|
|
|
|
print('No last known position available.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API
|
|
|
|
|
final place =
|
|
|
|
|
await _getCityAndCountry(position.latitude, position.longitude);
|
|
|
|
|
if (position != null) {
|
|
|
|
|
_getCityAndCountry(position!.latitude, position!.longitude);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -303,10 +332,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|
|
|
|
DateTime dateEvent = DateTime.now();
|
|
|
|
|
|
|
|
|
|
DateTime? pickedDate = await showDatePicker(
|
|
|
|
|
context: context,
|
|
|
|
|
firstDate: dateEvent,
|
|
|
|
|
initialDate: dateEvent,
|
|
|
|
|
lastDate: DateTime(2104));
|
|
|
|
|
context: context, firstDate: dateEvent, lastDate: DateTime(2104));
|
|
|
|
|
if (pickedDate == null) return;
|
|
|
|
|
Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
|
|
|
|
}
|
|
|
|
|