Add location permission request

This commit is contained in:
anhtunz
2024-12-18 22:38:26 +07:00
parent d0ba34381a
commit 9046b21831
11 changed files with 168 additions and 30 deletions

View File

@@ -11,6 +11,7 @@ import 'package:sfm_app/feature/home/home_bloc.dart';
import 'package:sfm_app/product/constant/app/app_constants.dart';
import 'package:sfm_app/product/constant/enums/app_route_enums.dart';
import 'package:sfm_app/product/constant/enums/role_enums.dart';
import 'package:sfm_app/product/permission/location_permission.dart';
import '../devices/devices_manager_bloc.dart';
import '../devices/devices_manager_screen.dart';
import '../home/home_screen.dart';
@@ -39,7 +40,7 @@ class MainScreen extends StatefulWidget {
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
APIServices apiServices = APIServices();
late MainBloc mainBloc;
bool isVN = true;
@@ -68,16 +69,39 @@ class _MainScreenState extends State<MainScreen> {
mainBloc.sinkIsVNIcon.add(isVN);
mainBloc.sinkThemeMode.add(isLight);
log("role: $role");
LocationPermissionRequest.instance.checkLocationPermission(context);
}
@override
void initState() {
super.initState();
mainBloc = BlocProvider.of(context);
WidgetsBinding.instance.addObserver(this);
initialCheck();
getBellNotification();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive) {
log("App Inactive");
} else if (state == AppLifecycleState.resumed) {
log("App Resumed");
LocationPermissionRequest.instance.checkLocationPermission(context);
} else if (state == AppLifecycleState.paused) {
log("App paused");
} else if (state == AppLifecycleState.detached) {
log("App detached");
}
}
@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
@override
Widget build(BuildContext context) {
ThemeNotifier themeNotifier = context.watch<ThemeNotifier>();

View File

@@ -17,8 +17,8 @@ class MapBloc extends BlocBase {
void dispose() {}
final mapType = StreamController<MapType>.broadcast();
StreamSink<MapType> get sinkmapType => mapType.sink;
Stream<MapType> get streammapType => mapType.stream;
StreamSink<MapType> get sinkMapType => mapType.sink;
Stream<MapType> get streamMapType => mapType.stream;
final mapController = StreamController<GoogleMapController>.broadcast();
StreamSink<GoogleMapController> get sinkmapController => mapController.sink;
@@ -71,4 +71,6 @@ class MapBloc extends BlocBase {
context, "Không tìm thấy đường", Colors.orange, Colors.white);
}
}
}

View File

@@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:sfm_app/feature/devices/device_model.dart';
@@ -10,6 +11,7 @@ import 'package:sfm_app/feature/map/map_bloc.dart';
import 'package:sfm_app/feature/map/widget/on_tap_marker_widget.dart';
import 'package:sfm_app/product/base/bloc/base_bloc.dart';
import 'package:sfm_app/product/constant/icon/icon_constants.dart';
import 'package:sfm_app/product/permission/location_permission.dart';
import 'package:sfm_app/product/services/api_services.dart';
class MapScreen extends StatefulWidget {
@@ -19,7 +21,7 @@ class MapScreen extends StatefulWidget {
State<MapScreen> createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
class _MapScreenState extends State<MapScreen> with WidgetsBindingObserver {
late BitmapDescriptor normalIcon;
late BitmapDescriptor offlineIcon;
late BitmapDescriptor abnormalIcon;
@@ -47,7 +49,8 @@ class _MapScreenState extends State<MapScreen> {
Set<Marker> markersAll = {};
List<Marker> markers = [];
LatLng myLocation = const LatLng(213761, 123123);
Position? position;
bool isAllowLocationPermission = false;
@override
void initState() {
super.initState();
@@ -143,17 +146,27 @@ class _MapScreenState extends State<MapScreen> {
cluster.getId(),
),
position: cluster.location,
onTap: () {
onTapMarker(
onTap: () async {
bool check = await checkLocationPermission(context);
if (check == true) {
Position position = await Geolocator.getCurrentPosition();
// ignore: use_build_context_synchronously
onTapMarker(
context,
_controller,
mapBloc,
myLocation,
LatLng(position.latitude, position.longitude),
cluster.items,
imageAssets,
markers,
hospitalIcon,
fireStationIcon);
fireStationIcon,
);
} else {
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Cannot get your Location")));
}
},
icon: getMarkerIcon(cluster),
);
@@ -234,4 +247,10 @@ class _MapScreenState extends State<MapScreen> {
}
}
}
Future<bool> checkLocationPermission(context) async {
bool check = await LocationPermissionRequest.instance
.checkLocationPermission(context);
return check;
}
}

View File

@@ -1,7 +1,6 @@
// ignore_for_file: use_build_context_synchronously
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'show_direction_widget.dart';
@@ -26,7 +25,6 @@ onTapMarker(
BitmapDescriptor hospitalIcon,
BitmapDescriptor fireStationIcon,
) {
LatLng testLocation = const LatLng(20.985453, 105.738381);
showModalBottomSheet(
context: context,
builder: (BuildContext modalBottomSheetContext) {
@@ -77,7 +75,7 @@ onTapMarker(
mapBloc.findTheWay(
context,
controller,
testLocation,
myLocation,
destination,
);
String deviceLocations = await DeviceUtils.instance