张延森 преди 3 години
родител
ревизия
26b38b9651
променени са 11 файла, в които са добавени 140 реда и са изтрити 87 реда
  1. 6
    3
      android/app/build.gradle
  2. 16
    7
      android/app/proguard-rules.pro
  3. 0
    1
      lib/models/Store.dart
  4. 0
    15
      lib/models/User.dart
  5. 32
    20
      lib/models/app.dart
  6. 1
    1
      lib/pages/home/index.dart
  7. 0
    0
      lib/utils/amap/const_config.dart
  8. 38
    15
      lib/utils/location.dart
  9. 1
    1
      lib/widgets/amap/amap.dart
  10. 43
    22
      pubspec.lock
  11. 3
    2
      pubspec.yaml

+ 6
- 3
android/app/build.gradle Целия файл

@@ -57,18 +57,21 @@ android {
57 57
     dependencies {
58 58
 //        implementation 'com.google.android.material:material:1.5.0'
59 59
         implementation('com.amap.api:3dmap:9.1.0')
60
-//        implementation('com.amap.api:location:6.1.0')
60
+        implementation('com.amap.api:location:6.1.0')
61 61
     }
62 62
 
63 63
     buildTypes {
64
+        debug {
65
+            minifyEnabled true
66
+        }
64 67
         release {
65 68
             // TODO: Add your own signing config for the release build.
66 69
             // Signing with the debug keys for now, so `flutter run --release` works.
67 70
             signingConfig signingConfigs.debug
68 71
 
69 72
             //关闭混淆, 否则在运行release包后可能出现运行崩溃, TODO后续进行混淆配置
70
-            minifyEnabled false //删除无用代码
71
-            shrinkResources false //删除无用资源
73
+            minifyEnabled true //删除无用代码
74
+//            shrinkResources false //删除无用资源
72 75
         }
73 76
     }
74 77
 }

+ 16
- 7
android/app/proguard-rules.pro Целия файл

@@ -6,13 +6,22 @@
6 6
 #-keep class io.flutter.**  { *; }
7 7
 #-keep class io.flutter.plugins.**  { *; }
8 8
 
9
-#高德地图SDK配置
10
--keep   class com.amap.api.maps.**{*;}
11
--keep   class com.autonavi.**{*;}
12
--keep   class com.amap.api.trace.**{*;}
13
-
14
-#高德等位SDK配置
9
+-keep class com.amap.api.**{*;}
10
+#3D 地图 V5.0.0之后
11
+-keep class com.amap.api.maps.**{*;}
12
+-keep class com.autonavi.**{*;}
13
+-keep class com.amap.api.trace.**{*;}
14
+#定位
15 15
 -keep class com.amap.api.location.**{*;}
16 16
 -keep class com.amap.api.fence.**{*;}
17 17
 -keep class com.loc.**{*;}
18
--keep class com.autonavi.aps.amapapi.model.**{*;}
18
+-keep class com.autonavi.aps.amapapi.model.**{*;}
19
+
20
+##搜索
21
+#-keep class com.amap.api.services.**{*;}
22
+##2D地图
23
+#-keep class com.amap.api.maps2d.**{*;}
24
+#-keep class com.amap.api.mapcore2d.**{*;}
25
+##导航
26
+#-keep class com.amap.api.navi.**{*;}
27
+#-keep class com.autonavi.**{*;}

+ 0
- 1
lib/models/Store.dart Целия файл

@@ -1 +0,0 @@
1
-// TODO Implement this library.

+ 0
- 15
lib/models/User.dart Целия файл

@@ -1,15 +0,0 @@
1
-
2
-import 'package:farmer_client/models/entities/person.dart';
3
-import 'package:get/get.dart';
4
-
5
-class User extends GetxController {
6
-  Person? _person;
7
-  Person? get person => _person;
8
-  set person (Person? p) {
9
-    _person = p;
10
-    update();
11
-  }
12
-
13
-  bool get isLogin => _person != null;
14
-
15
-}

+ 32
- 20
lib/models/app.dart Целия файл

@@ -1,10 +1,11 @@
1 1
 
2
+import 'dart:async';
2 3
 import 'dart:io';
3 4
 
5
+import 'package:amap_flutter_location/amap_flutter_location.dart';
4 6
 import 'package:farmer_client/models/entities/person.dart';
7
+import 'package:farmer_client/widgets/Modal.dart';
5 8
 import 'package:get/get.dart';
6
-import 'package:location/location.dart';
7
-import '../services/user.dart';
8 9
 import '../utils/location.dart';
9 10
 
10 11
 class AppController extends GetxController {
@@ -12,12 +13,19 @@ class AppController extends GetxController {
12 13
   static AppController t = Get.find();
13 14
 
14 15
   final user = Rx<Person>(Person());
15
-  final location = Rxn<LocationData>();
16
+  final location = Rxn<Map<String, Object>>();
16 17
   final testInt = 1.obs;
17 18
 
19
+  AMapFlutterLocation? _location;
20
+  StreamSubscription<Map<String, Object>>? _locationListener;
21
+
18 22
   get locationStr {
19 23
     if (null == location.value) return null;
20
-    return location.value!.longitude.toString() + "," + location.value!.latitude.toString();
24
+
25
+    double longitude = location.value!['longitude'] as double;
26
+    double latitude = location.value!['latitude'] as double;
27
+
28
+    return longitude.toString() + "," + latitude.toString();
21 29
   }
22 30
 
23 31
   @override
@@ -26,28 +34,21 @@ class AppController extends GetxController {
26 34
 
27 35
     // 尝试获取 location
28 36
     requireLocation().then((loc) async {
29
-      var _data = await loc.getLocation();
30
-      location(_data);
37
+      _location = loc;
31 38
 
32 39
       // 监听位置变化
33
-      loc.onLocationChanged.listen((LocationData currentLocation) {
34
-        location(currentLocation);
40
+      _locationListener = loc.onLocationChanged().listen((Map<String, Object> result) {
41
+        location(result);
35 42
       });
43
+      loc.startLocation();
36 44
     }).catchError((e) {
37 45
       print(e);
38 46
 
39
-      var message = e.message.toString().contains('CANCELED')
40
-          ? '您取消了, 确定后将退出程序'
41
-          : e.message;
42
-
43
-      // Get.dialog();
44
-      // Get.defaultDialog(
45
-      //   title: '提示',
46
-      //   middleText: message,
47
-      //   radius: 8,
48
-      //   textConfirm: '确定',
49
-      //   onConfirm: () => exit(1),
50
-      // );
47
+      showAlert(
48
+        title: '获取定位失败',
49
+        message: e.message,
50
+        onConfirm: () => exit(1),
51
+      );
51 52
     });
52 53
 
53 54
     // // 尝试获取一次人员信息
@@ -58,4 +59,15 @@ class AppController extends GetxController {
58 59
     // });
59 60
   }
60 61
 
62
+  @override
63
+  void onClose() {
64
+    if (null != _locationListener) {
65
+      _locationListener?.cancel();
66
+    }
67
+
68
+    if (null != _location) {
69
+      _location?.destroy();
70
+    }
71
+  }
72
+
61 73
 }

+ 1
- 1
lib/pages/home/index.dart Целия файл

@@ -20,7 +20,7 @@ class Home extends BasicPage {
20 20
       //   return Modal(type: 'dialog', title: '测试', message: '这是一段 message !', onConfirm: () => print('hahahaha'),);
21 21
       // } );
22 22
       // Get.dialog(Modal(type: 'dialog', title: '测试', message: '这是一段 message !', onConfirm: () => print('hahahaha'),));
23
-      modal.showDialog(title: '测试', content: SizedBox(width: 300, height: 300,), onConfirm: () => false);
23
+      modal.showDialog(title: '测试', content: SizedBox(width: 400, height: 300,), onConfirm: () => false);
24 24
     }, 2000);
25 25
     
26 26
     return const HomePage();

lib/widgets/amap/const_config.dart → lib/utils/amap/const_config.dart Целия файл


+ 38
- 15
lib/utils/location.dart Целия файл

@@ -1,25 +1,48 @@
1
-import 'package:location/location.dart';
1
+import 'dart:async';
2
+
3
+import 'package:amap_flutter_location/amap_flutter_location.dart';
4
+import 'package:amap_flutter_location/amap_location_option.dart';
5
+import 'package:permission_handler/permission_handler.dart';
6
+
7
+import 'amap/const_config.dart';
2 8
 
3 9
 Future requireLocation() async {
4
-  Location location = Location();
5
-  bool _serviceEnabled;
6
-  PermissionStatus _permissionGranted;
7
-
8
-  _serviceEnabled = await location.serviceEnabled();
9
-  if (!_serviceEnabled) {
10
-    _serviceEnabled = await location.requestService();
11
-    if (!_serviceEnabled) {
12
-      throw Exception("请开启系统定位功能");
13
-    }
14
-  }
10
+  AMapLocationOption locationOption = AMapLocationOption();
11
+  final AMapFlutterLocation location = AMapFlutterLocation();
12
+  location.setLocationOption(locationOption);
15 13
 
16
-  _permissionGranted = await location.hasPermission();
14
+  /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作
15
+  ///
16
+  /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
17
+  /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
18
+  ///
19
+  /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
20
+  ///
21
+  /// [hasContains] 隐私声明中是否包含高德隐私政策说明
22
+  ///
23
+  /// [hasShow] 隐私权政策是否弹窗展示告知用户
24
+  AMapFlutterLocation.updatePrivacyShow(true, true);
25
+
26
+  /// 设置是否已经取得用户同意,如果未取得用户同意,高德定位SDK将不会工作
27
+  ///
28
+  /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
29
+  ///
30
+  /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
31
+  ///
32
+  /// [hasAgree] 隐私权政策是否已经取得用户同意
33
+  AMapFlutterLocation.updatePrivacyAgree(true);
34
+
35
+  // 判断定位权限
36
+  PermissionStatus _permissionGranted = await Permission.location.status;
17 37
   if (_permissionGranted == PermissionStatus.denied) {
18
-    _permissionGranted = await location.requestPermission();
38
+    // 如果未允许, 尝试申请一次
39
+    _permissionGranted = await Permission.location.request();
19 40
     if (_permissionGranted != PermissionStatus.granted) {
20 41
       throw Exception("请设置允许当前程序定位功能");
21 42
     }
22 43
   }
23 44
 
45
+  AMapFlutterLocation.setApiKey(ConstConfig.amapApiKeys.androidKey!, ConstConfig.amapApiKeys.iosKey!);
46
+
24 47
   return location;
25
-}
48
+}

+ 1
- 1
lib/widgets/amap/amap.dart Целия файл

@@ -2,7 +2,7 @@ import 'package:amap_flutter_map/amap_flutter_map.dart';
2 2
 import 'package:amap_flutter_base/amap_flutter_base.dart';
3 3
 import 'package:flutter/widgets.dart';
4 4
 
5
-import 'const_config.dart';
5
+import 'package:farmer_client/utils/amap/const_config.dart';
6 6
 
7 7
 // https://developer.amap.com/api/flutter/guide/map-flutter-plug-in/map-flutter-info
8 8
 class AMap extends StatelessWidget {

+ 43
- 22
pubspec.lock Целия файл

@@ -15,6 +15,13 @@ packages:
15 15
       url: "https://pub.flutter-io.cn"
16 16
     source: hosted
17 17
     version: "3.0.0"
18
+  amap_flutter_location:
19
+    dependency: "direct main"
20
+    description:
21
+      name: amap_flutter_location
22
+      url: "https://pub.flutter-io.cn"
23
+    source: hosted
24
+    version: "3.0.0"
18 25
   amap_flutter_map:
19 26
     dependency: "direct main"
20 27
     description:
@@ -70,7 +77,7 @@ packages:
70 77
       name: carousel_slider
71 78
       url: "https://pub.flutter-io.cn"
72 79
     source: hosted
73
-    version: "4.0.0"
80
+    version: "4.1.1"
74 81
   characters:
75 82
     dependency: transitive
76 83
     description:
@@ -273,27 +280,6 @@ packages:
273 280
       url: "https://pub.flutter-io.cn"
274 281
     source: hosted
275 282
     version: "1.0.1"
276
-  location:
277
-    dependency: "direct main"
278
-    description:
279
-      name: location
280
-      url: "https://pub.flutter-io.cn"
281
-    source: hosted
282
-    version: "4.3.0"
283
-  location_platform_interface:
284
-    dependency: transitive
285
-    description:
286
-      name: location_platform_interface
287
-      url: "https://pub.flutter-io.cn"
288
-    source: hosted
289
-    version: "2.3.0"
290
-  location_web:
291
-    dependency: transitive
292
-    description:
293
-      name: location_web
294
-      url: "https://pub.flutter-io.cn"
295
-    source: hosted
296
-    version: "3.1.1"
297 283
   logging:
298 284
     dependency: transitive
299 285
     description:
@@ -385,6 +371,41 @@ packages:
385 371
       url: "https://pub.flutter-io.cn"
386 372
     source: hosted
387 373
     version: "2.0.5"
374
+  permission_handler:
375
+    dependency: "direct main"
376
+    description:
377
+      name: permission_handler
378
+      url: "https://pub.flutter-io.cn"
379
+    source: hosted
380
+    version: "9.2.0"
381
+  permission_handler_android:
382
+    dependency: transitive
383
+    description:
384
+      name: permission_handler_android
385
+      url: "https://pub.flutter-io.cn"
386
+    source: hosted
387
+    version: "9.0.2+1"
388
+  permission_handler_apple:
389
+    dependency: transitive
390
+    description:
391
+      name: permission_handler_apple
392
+      url: "https://pub.flutter-io.cn"
393
+    source: hosted
394
+    version: "9.0.4"
395
+  permission_handler_platform_interface:
396
+    dependency: transitive
397
+    description:
398
+      name: permission_handler_platform_interface
399
+      url: "https://pub.flutter-io.cn"
400
+    source: hosted
401
+    version: "3.7.0"
402
+  permission_handler_windows:
403
+    dependency: transitive
404
+    description:
405
+      name: permission_handler_windows
406
+      url: "https://pub.flutter-io.cn"
407
+    source: hosted
408
+    version: "0.1.0"
388 409
   platform:
389 410
     dependency: transitive
390 411
     description:

+ 3
- 2
pubspec.yaml Целия файл

@@ -44,10 +44,11 @@ dependencies:
44 44
   flutter_screenutil: ^5.3.1
45 45
   get_storage: ^2.0.3
46 46
   fluttertoast: ^8.0.9
47
-  location: ^4.3.0
47
+#  location: ^4.3.0
48 48
   amap_flutter_map: ^3.0.0
49
-  #  amap_flutter_location: ^3.0.0
49
+  amap_flutter_location: ^3.0.0
50 50
   carousel_slider: ^4.0.0
51
+  permission_handler: ^9.2.0
51 52
 
52 53
 
53 54