Browse Source

增加定位开关和定位权限的检查,并跳转设置页.

maqiang 7 months ago
parent
commit
fe53eee001

+ 1 - 1
android/settings.gradle

@@ -20,7 +20,7 @@ pluginManagement {
20
 plugins {
20
 plugins {
21
     id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21
     id "dev.flutter.flutter-plugin-loader" version "1.0.0"
22
     id "com.android.application" version "7.3.0" apply false
22
     id "com.android.application" version "7.3.0" apply false
23
-    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
23
+    id "org.jetbrains.kotlin.android" version "1.8.0" apply false
24
 }
24
 }
25
 
25
 
26
 include ":app"
26
 include ":app"

+ 2 - 7
lib/page/print/print_page.dart

@@ -1,13 +1,9 @@
1
 import 'dart:async';
1
 import 'dart:async';
2
-import 'dart:convert';
3
 
2
 
4
 import 'package:amap_flutter_location/amap_location_option.dart';
3
 import 'package:amap_flutter_location/amap_location_option.dart';
5
 import 'package:flutter/material.dart';
4
 import 'package:flutter/material.dart';
6
 import 'package:flutter/services.dart';
5
 import 'package:flutter/services.dart';
7
 import 'package:lszlgl/base/base_lifecycle_state.dart';
6
 import 'package:lszlgl/base/base_lifecycle_state.dart';
8
-import 'package:lszlgl/page/print/connect_print_page.dart';
9
-import 'package:signature/signature.dart';
10
-import 'package:lszlgl/widget/button.dart';
11
 
7
 
12
 import '../../drfit/database.dart';
8
 import '../../drfit/database.dart';
13
 import '../../drfit/model_factory.dart';
9
 import '../../drfit/model_factory.dart';
@@ -88,7 +84,7 @@ class _PrintPageState extends BaseLifecycleState<PrintPage> {
88
     }
84
     }
89
 
85
 
90
     final codec = await ui.instantiateImageCodec(
86
     final codec = await ui.instantiateImageCodec(
91
-      widget.args.bytes!,
87
+      widget.args.bytes,
92
       targetHeight: 590,  // 640
88
       targetHeight: 590,  // 640
93
       targetWidth: targetWidth, // 560
89
       targetWidth: targetWidth, // 560
94
     );
90
     );
@@ -173,9 +169,8 @@ class _PrintPageState extends BaseLifecycleState<PrintPage> {
173
   }
169
   }
174
 
170
 
175
   void getLocation() async {
171
   void getLocation() async {
176
-    bool granted = await LocationUtils.requestLocationPermission();
172
+    bool granted = await LocationUtils.checkLocationAvailable();
177
     if (!granted) {
173
     if (!granted) {
178
-      MyNavigator.showToast('请打开APP的定位权限');
179
       return;
174
       return;
180
     }
175
     }
181
     LocationUtils.setLocationOption(AMapLocationOption(onceLocation: true));
176
     LocationUtils.setLocationOption(AMapLocationOption(onceLocation: true));

+ 1 - 3
lib/page/sample_task/reap_sample_detail/reap_sample_basic_detail_page.dart

@@ -17,7 +17,6 @@ import '../../../config/colors.dart';
17
 import '../../../model/rsp/dict_rsp.dart';
17
 import '../../../model/rsp/dict_rsp.dart';
18
 import '../../../model/rsp/sample_task_rsp.dart';
18
 import '../../../model/rsp/sample_task_rsp.dart';
19
 import '../../../network/my_api.dart';
19
 import '../../../network/my_api.dart';
20
-import '../../../service/dict_service.dart';
21
 import '../../../service/user_service.dart';
20
 import '../../../service/user_service.dart';
22
 import '../../../utils/file_utils.dart';
21
 import '../../../utils/file_utils.dart';
23
 import '../../../widget/card_item.dart';
22
 import '../../../widget/card_item.dart';
@@ -158,9 +157,8 @@ class _ReapSampleBasicDetailPageState extends BaseLifecycleState<ReapSampleBasic
158
   }
157
   }
159
 
158
 
160
   void getLocation() async {
159
   void getLocation() async {
161
-    bool granted = await LocationUtils.requestLocationPermission();
160
+    bool granted = await LocationUtils.checkLocationAvailable();
162
     if (!granted) {
161
     if (!granted) {
163
-      MyNavigator.showToast('请打开APP的定位权限');
164
       return;
162
       return;
165
     }
163
     }
166
     LocationUtils.setLocationOption(AMapLocationOption(onceLocation: true));
164
     LocationUtils.setLocationOption(AMapLocationOption(onceLocation: true));

+ 53 - 12
lib/utils/location_utils.dart

@@ -1,7 +1,12 @@
1
 import 'package:amap_flutter_location/amap_flutter_location.dart';
1
 import 'package:amap_flutter_location/amap_flutter_location.dart';
2
 import 'package:amap_flutter_location/amap_location_option.dart';
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
 import 'package:permission_handler/permission_handler.dart';
5
 import 'package:permission_handler/permission_handler.dart';
4
 
6
 
7
+import '../router/my_navigator.dart';
8
+import '../widget/button.dart';
9
+
5
 class LocationUtils {
10
 class LocationUtils {
6
   LocationUtils._();
11
   LocationUtils._();
7
 
12
 
@@ -110,22 +115,58 @@ class LocationUtils {
110
     return location.onLocationChanged();
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
     var status = await Permission.location.status;
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
       status = await Permission.location.request();
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
         return false;
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
 }

+ 2 - 0
pubspec.yaml

@@ -93,6 +93,8 @@ dependencies:
93
   drift: ^2.18.0
93
   drift: ^2.18.0
94
   #
94
   #
95
   sqlite3_flutter_libs: ^0.5.0
95
   sqlite3_flutter_libs: ^0.5.0
96
+  # 打开系统设置
97
+  app_settings: ^5.1.1
96
 
98
 
97
 dev_dependencies:
99
 dev_dependencies:
98
   flutter_test:
100
   flutter_test: