Compare commits
4 Commits
cdae84090f
...
f693ffa6b8
Author | SHA1 | Date | |
---|---|---|---|
f693ffa6b8 | |||
93beabe884 | |||
a7d8bebe1f | |||
4a313fc725 |
@ -1,3 +1,4 @@
|
|||||||
|
import 'package:covas_mobile/classes/alert.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
|
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
|
||||||
@ -51,21 +52,37 @@ 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;
|
PermissionStatus status = await Permission.location.status;
|
||||||
|
final List<Events> body = [];
|
||||||
var url = Uri.parse("${globals.api}/events");
|
var url = Uri.parse("${globals.api}/events");
|
||||||
if (status.isGranted) {
|
if (status.isGranted) {
|
||||||
print("Location permission granted");
|
print("Location permission granted");
|
||||||
|
|
||||||
// Get the current position with high accuracy
|
// 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(
|
const LocationSettings locationSettings = 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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (position != null) {
|
||||||
// Calculate the boundaries
|
// Calculate the boundaries
|
||||||
double radiusInKm = 50;
|
double radiusInKm = 50;
|
||||||
double latDistance = radiusInKm / 111.0;
|
double latDistance = radiusInKm / 111.0;
|
||||||
@ -83,7 +100,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
}
|
}
|
||||||
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 = [];
|
|
||||||
if (accessToken.isNotEmpty) {
|
if (accessToken.isNotEmpty) {
|
||||||
final response = await http.get(url, headers: {
|
final response = await http.get(url, headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -92,6 +109,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
final List body = json.decode(utf8.decode(response.bodyBytes));
|
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||||
return body.map((e) => Events.fromJson(e)).toList();
|
return body.map((e) => Events.fromJson(e)).toList();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,8 +137,6 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
// Initialize data fetch when the page loads
|
// Initialize data fetch when the page loads
|
||||||
_getCurrentLocation();
|
_getCurrentLocation();
|
||||||
|
|
||||||
Datepicker.text = DateFormat("dd-MM-yyyy").format(new DateTime.now());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the device's current location
|
// Get the device's current location
|
||||||
@ -131,19 +147,32 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
print("Location permission granted");
|
print("Location permission granted");
|
||||||
|
|
||||||
// Get the current position with high accuracy
|
// Get the current position with high accuracy
|
||||||
LocationSettings locationSettings = LocationSettings(
|
const LocationSettings locationSettings = LocationSettings(
|
||||||
accuracy: LocationAccuracy.high,
|
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
|
||||||
distanceFilter:
|
Position? position;
|
||||||
10, // Optional: Minimum distance (in meters) to trigger location update
|
try {
|
||||||
);
|
position = await Geolocator.getCurrentPosition(
|
||||||
|
locationSettings: locationSettings);
|
||||||
Position position = await Geolocator.getCurrentPosition(
|
} on LocationServiceDisabledException {
|
||||||
locationSettings: locationSettings,
|
// 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
|
// Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API
|
||||||
final place =
|
if (position != null) {
|
||||||
await _getCityAndCountry(position.latitude, position.longitude);
|
_getCityAndCountry(position!.latitude, position!.longitude);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,10 +332,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
DateTime dateEvent = DateTime.now();
|
DateTime dateEvent = DateTime.now();
|
||||||
|
|
||||||
DateTime? pickedDate = await showDatePicker(
|
DateTime? pickedDate = await showDatePicker(
|
||||||
context: context,
|
context: context, firstDate: dateEvent, lastDate: DateTime(2104));
|
||||||
firstDate: dateEvent,
|
|
||||||
initialDate: dateEvent,
|
|
||||||
lastDate: DateTime(2104));
|
|
||||||
if (pickedDate == null) return;
|
if (pickedDate == null) return;
|
||||||
Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
||||||
}
|
}
|
||||||
|
@ -141,19 +141,33 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
|
|||||||
"Location permissions are permanently denied. Enable them in settings.");
|
"Location permissions are permanently denied. Enable them in settings.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LocationSettings locationSettings = LocationSettings(
|
const LocationSettings locationSettings = LocationSettings(
|
||||||
accuracy: LocationAccuracy.high,
|
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
|
||||||
distanceFilter:
|
Position? position;
|
||||||
10, // Optional: Minimum distance (in meters) to trigger location update
|
try {
|
||||||
);
|
position = await Geolocator.getCurrentPosition(
|
||||||
|
locationSettings: locationSettings);
|
||||||
Position position = await Geolocator.getCurrentPosition(
|
} on LocationServiceDisabledException {
|
||||||
locationSettings: locationSettings,
|
// 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) {
|
||||||
setState(() {
|
setState(() {
|
||||||
userPosition = LatLng(position.latitude, position.longitude);
|
userPosition = LatLng(position!.latitude, position!.longitude);
|
||||||
isUserPositionInitialized = true;
|
isUserPositionInitialized = true;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
_initToken();
|
_initToken();
|
||||||
_getEventInfo();
|
_getEventInfo();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user