added map live locating

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

@ -41,4 +41,6 @@
<data android:mimeType="text/plain"/>
</intent>
</queries>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>

@ -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();
}
}

@ -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(),
);
},

@ -121,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
@ -176,6 +184,54 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
geolocator:
dependency: "direct main"
description:
name: geolocator
sha256: "694ec58afe97787b5b72b8a0ab78c1a9244811c3c10e72c4362ef3c0ceb005cd"
url: "https://pub.dev"
source: hosted
version: "11.0.0"
geolocator_android:
dependency: transitive
description:
name: geolocator_android
sha256: f15d1536cd01b1399578f1da1eb5d566e7a718db6a3648f2c24d2e2f859f0692
url: "https://pub.dev"
source: hosted
version: "4.5.4"
geolocator_apple:
dependency: transitive
description:
name: geolocator_apple
sha256: bc2aca02423ad429cb0556121f56e60360a2b7d694c8570301d06ea0c00732fd
url: "https://pub.dev"
source: hosted
version: "2.3.7"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
sha256: "009a21c4bc2761e58dccf07c24f219adaebe0ff707abdfd40b0a763d4003fab9"
url: "https://pub.dev"
source: hosted
version: "4.2.2"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
sha256: "49d8f846ebeb5e2b6641fe477a7e97e5dd73f03cbfef3fd5c42177b7300fb0ed"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
geolocator_windows:
dependency: transitive
description:
name: geolocator_windows
sha256: "53da08937d07c24b0d9952eb57a3b474e29aae2abf9dd717f7e1230995f13f0e"
url: "https://pub.dev"
source: hosted
version: "0.2.3"
html:
dependency: transitive
description:
@ -344,6 +400,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.0.2"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
pointycastle:
dependency: transitive
description:
@ -389,6 +453,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.10.0"
sprintf:
dependency: transitive
description:
name: sprintf
sha256: "1fc9ffe69d4df602376b52949af107d8f5703b77cda567c4d7d86a0693120f23"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
stack_trace:
dependency: transitive
description:
@ -453,6 +525,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.2"
uuid:
dependency: transitive
description:
name: uuid
sha256: cd210a09f7c18cbe5a02511718e0334de6559871052c90a90c0cca46a4aa81c8
url: "https://pub.dev"
source: hosted
version: "4.3.3"
vector_math:
dependency: transitive
description:

@ -42,6 +42,7 @@ dependencies:
latlong2: ^0.9.0
http: ^1.2.1
flutter_timer_countdown: ^1.0.7
geolocator: ^11.0.0
dev_dependencies:
flutter_test: