|
@@ -1,7 +1,12 @@
|
1
|
1
|
import 'package:amap_flutter_location/amap_flutter_location.dart';
|
2
|
2
|
import 'package:amap_flutter_location/amap_location_option.dart';
|
|
3
|
+import 'package:app_settings/app_settings.dart';
|
|
4
|
+import 'package:flutter/material.dart';
|
3
|
5
|
import 'package:permission_handler/permission_handler.dart';
|
4
|
6
|
|
|
7
|
+import '../router/my_navigator.dart';
|
|
8
|
+import '../widget/button.dart';
|
|
9
|
+
|
5
|
10
|
class LocationUtils {
|
6
|
11
|
LocationUtils._();
|
7
|
12
|
|
|
@@ -110,22 +115,58 @@ class LocationUtils {
|
110
|
115
|
return location.onLocationChanged();
|
111
|
116
|
}
|
112
|
117
|
|
113
|
|
- /// 申请定位权限
|
114
|
|
- /// 授予定位权限返回true, 否则返回false
|
115
|
|
- static Future<bool> requestLocationPermission() async {
|
116
|
|
- //获取当前的权限
|
|
118
|
+ /// 检查定位是否可用
|
|
119
|
+ /// 1.app定位权限
|
|
120
|
+ /// 2.系统定位开关
|
|
121
|
+ static Future<bool> checkLocationAvailable() async {
|
|
122
|
+ // 检查定位权限
|
117
|
123
|
var status = await Permission.location.status;
|
118
|
|
- if (status == PermissionStatus.granted) {
|
119
|
|
- //已经授权
|
120
|
|
- return true;
|
121
|
|
- } else {
|
122
|
|
- //未授权则发起一次申请
|
|
124
|
+ if (status != PermissionStatus.granted) {
|
|
125
|
+ //未授权 申请定位权限
|
123
|
126
|
status = await Permission.location.request();
|
124
|
|
- if (status == PermissionStatus.granted) {
|
125
|
|
- return true;
|
126
|
|
- } else {
|
|
127
|
+ if (status != PermissionStatus.granted) {
|
|
128
|
+ // 未授权 提示用户去设置
|
|
129
|
+ MyNavigator.showToast('请打开APP的定位权限');
|
|
130
|
+ showSettingDialog(
|
|
131
|
+ title: '定位权限',
|
|
132
|
+ content: '为了APP能使用位置功能,您在系统设置中授权APP的定位权限。',
|
|
133
|
+ onTap: openAppSettings,
|
|
134
|
+ );
|
127
|
135
|
return false;
|
128
|
136
|
}
|
129
|
137
|
}
|
|
138
|
+ // 检查系统定位开关
|
|
139
|
+ bool locationEnable = await Permission.location.serviceStatus.isEnabled;
|
|
140
|
+ if (!locationEnable) {
|
|
141
|
+ showSettingDialog(
|
|
142
|
+ title: '定位功能',
|
|
143
|
+ content: '为了APP能使用位置功能,您在系统设置中打开定位开关。',
|
|
144
|
+ onTap: () => AppSettings.openAppSettings(type: AppSettingsType.location),
|
|
145
|
+ );
|
|
146
|
+ return false;
|
|
147
|
+ }
|
|
148
|
+ return true;
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ static void showSettingDialog({String? title, String? content, VoidCallback? onTap}) {
|
|
152
|
+ MyNavigator.showDialog(
|
|
153
|
+ tag: 'setting',
|
|
154
|
+ builder: (_) => AlertDialog(
|
|
155
|
+ title: title != null ? Text(title) : null,
|
|
156
|
+ content: content != null ? Text(content) : null,
|
|
157
|
+ actions: [
|
|
158
|
+ MyButton(
|
|
159
|
+ '去设置',
|
|
160
|
+ alignment: null,
|
|
161
|
+ backgroundColor: const Color(0xFFCE615A),
|
|
162
|
+ onTap: () {
|
|
163
|
+ onTap?.call();
|
|
164
|
+ MyNavigator.dismiss(tag: 'setting');
|
|
165
|
+ },
|
|
166
|
+ ),
|
|
167
|
+ MyButton('取消', alignment: null, onTap: () => MyNavigator.dismiss(tag: 'setting')),
|
|
168
|
+ ],
|
|
169
|
+ ),
|
|
170
|
+ );
|
130
|
171
|
}
|
131
|
172
|
}
|