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

View File

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

View File

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

View File

@ -1,4 +1,3 @@
import 'package:app/extensions/obtainProviderExtension.dart'; import 'package:app/extensions/obtainProviderExtension.dart';
import 'package:app/state/mapState.dart'; import 'package:app/state/mapState.dart';
import 'package:app/state/timeStatusState.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/status.dart';
import 'package:app/view/map.dart'; import 'package:app/view/map.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong2/latlong.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class HomeView extends StatefulWidget { class HomeView extends StatefulWidget {
@ -18,6 +19,30 @@ class HomeView extends StatefulWidget {
} }
class _HomePageState extends State<HomeView> { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -43,8 +68,25 @@ class _HomePageState extends State<HomeView> {
], ],
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: WatchState<MapState>((context, state) => Icon(state.followLocation ? Icons.my_location : Icons.location_disabled)), child: WatchState<MapState>((context, state) => Icon(
onPressed: () => context.obtainState<MapState>().toggleLocationLock(), 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( body: Column(
children: [ children: [
@ -61,7 +103,7 @@ class _HomePageState extends State<HomeView> {
builder: (context, state, child) { builder: (context, state, child) {
return LoadingContainer( return LoadingContainer(
loading: child == null, loading: child == null,
fetching: state.isCurrentlyFetching, fetching: state.isCurrentlyLoading,
child: const MapView(), child: const MapView(),
); );
}, },

View File

@ -121,6 +121,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.1" version: "1.3.1"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -176,6 +184,54 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: html:
dependency: transitive dependency: transitive
description: description:
@ -344,6 +400,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.0.2" 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: pointycastle:
dependency: transitive dependency: transitive
description: description:
@ -389,6 +453,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.10.0" version: "1.10.0"
sprintf:
dependency: transitive
description:
name: sprintf
sha256: "1fc9ffe69d4df602376b52949af107d8f5703b77cda567c4d7d86a0693120f23"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
@ -453,6 +525,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.2.2" version: "2.2.2"
uuid:
dependency: transitive
description:
name: uuid
sha256: cd210a09f7c18cbe5a02511718e0334de6559871052c90a90c0cca46a4aa81c8
url: "https://pub.dev"
source: hosted
version: "4.3.3"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:

View File

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