added map live locating

This commit is contained in:
2024-03-27 01:03:01 +01:00
parent 99cc98f853
commit 911c2738aa
5 changed files with 136 additions and 15 deletions

View File

@ -1,22 +1,18 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geolocator/geolocator.dart';
class MapState extends ChangeNotifier {
bool _isLocationLock = false;
bool _isCurrentlyFetchin = false;
bool _isCurrentlyLoading = false;
final MapController _mapController = MapController();
final Geolocator _geolocator = Geolocator();
bool get followLocation => _isLocationLock;
bool get isCurrentlyFetching => _isCurrentlyFetchin;
bool get isCurrentlyLoading => _isCurrentlyLoading;
MapController get getMapController => _mapController;
Geolocator get getGeolocator => _geolocator;
toggleLocationLock() {
_isLocationLock = !_isLocationLock;
notifyListeners();
}
set setNetworkActivity(bool active) {
_isCurrentlyFetchin = active;
void setLoading(bool loading) {
_isCurrentlyLoading = loading;
notifyListeners();
}
}

View File

@ -1,4 +1,3 @@
import 'package:app/extensions/obtainProviderExtension.dart';
import 'package:app/state/mapState.dart';
import 'package:app/state/timeStatusState.dart';
@ -8,6 +7,8 @@ import 'package:app/view/appInfo.dart';
import 'package:app/view/status.dart';
import 'package:app/view/map.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong2/latlong.dart';
import 'package:provider/provider.dart';
class HomeView extends StatefulWidget {
@ -18,6 +19,30 @@ class HomeView extends StatefulWidget {
}
class _HomePageState extends State<HomeView> {
bool locationEnabled = false;
Future<LatLng?> getLatLng() async {
LocationPermission permissionStatus = await Geolocator.checkPermission();
if (permissionStatus == LocationPermission.denied) {
permissionStatus = await Geolocator.requestPermission();
if (permissionStatus == LocationPermission.denied || permissionStatus == LocationPermission.deniedForever) {
return null;
}
}
Position? position;
try {
position = await Geolocator.getCurrentPosition();
} catch (e) {
position = await Geolocator.getLastKnownPosition();
}
if (position != null) {
return LatLng(position.latitude, position.longitude);
}
return null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -43,8 +68,25 @@ class _HomePageState extends State<HomeView> {
],
),
floatingActionButton: FloatingActionButton(
child: WatchState<MapState>((context, state) => Icon(state.followLocation ? Icons.my_location : Icons.location_disabled)),
onPressed: () => context.obtainState<MapState>().toggleLocationLock(),
child: WatchState<MapState>((context, state) => Icon(
locationEnabled
? state.isCurrentlyLoading
? Icons.location_searching
: Icons.my_location
: Icons.location_disabled
)),
onPressed: () async {
MapState mapState = context.obtainState<MapState>();
mapState.setLoading(true);
LatLng? latLng = await getLatLng();
if (latLng != null) {
mapState.getMapController.move(latLng, 16);
setState(() {
locationEnabled = true;
});
}
mapState.setLoading(false);
},
),
body: Column(
children: [
@ -61,7 +103,7 @@ class _HomePageState extends State<HomeView> {
builder: (context, state, child) {
return LoadingContainer(
loading: child == null,
fetching: state.isCurrentlyFetching,
fetching: state.isCurrentlyLoading,
child: const MapView(),
);
},