张延森 3 年前
父节点
当前提交
aab895cc95

+ 2
- 6
lib/main.dart 查看文件

@@ -1,21 +1,16 @@
1
-import 'dart:async';
2 1
 
3
-import 'package:farmer_client/models/Store.dart';
4
-import 'package:farmer_client/pages/home/index.dart';
5
-import 'package:farmer_client/pages/splash/splash.dart';
6
-import 'package:farmer_client/pages/login/login.dart';
7 2
 import 'package:flutter/material.dart';
8 3
 import 'package:get/get.dart';
9 4
 import 'package:get_storage/get_storage.dart';
10 5
 import 'package:flutter_screenutil/flutter_screenutil.dart';
11 6
 
7
+import 'models/index.dart';
12 8
 import 'routes/index.dart';
13 9
 import 'widgets/OrderListCard.dart';
14 10
 
15 11
 
16 12
 void main() async {
17 13
   await GetStorage.init();
18
-  Store().init();
19 14
 
20 15
   runApp(MyApp());
21 16
 }
@@ -37,6 +32,7 @@ class MyApp extends StatelessWidget {
37 32
           initialRoute: '/splash',
38 33
           defaultTransition: Transition.native,
39 34
           routingCallback: routingCallback,
35
+          initialBinding: AppBindings(),
40 36
           getPages: pages,
41 37
         );
42 38
       },

+ 0
- 11
lib/models/Store.dart 查看文件

@@ -1,11 +0,0 @@
1
-
2
-import 'package:farmer_client/models/User.dart';
3
-import 'package:get/get.dart';
4
-
5
-class Store {
6
-  Store();
7
-
8
-  init() {
9
-    Get.put(User(), tag: 'user');
10
-  }
11
-}

+ 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
-}

+ 47
- 0
lib/models/app.dart 查看文件

@@ -0,0 +1,47 @@
1
+
2
+import 'dart:io';
3
+
4
+import 'package:farmer_client/models/entities/person.dart';
5
+import 'package:get/get.dart';
6
+import 'package:location/location.dart';
7
+import '../services/user.dart';
8
+import '../utils/location.dart';
9
+
10
+class AppController extends GetxController {
11
+  // 有了这句, 可以直接 AppController.t 调用
12
+  static AppController t = Get.find();
13
+
14
+  final user = Person().obs;
15
+  final location = Rxn<LocationData>();
16
+  final testInt = 1.obs;
17
+
18
+  @override
19
+  void onInit() {
20
+    super.onInit();
21
+
22
+    // 尝试获取 location
23
+    requireLocation().then((loc) async {
24
+      var _data = await loc.getLocation();
25
+      location(_data);
26
+
27
+      // 监听位置变化
28
+      loc.onLocationChanged.listen((LocationData currentLocation) {
29
+        location(currentLocation);
30
+      });
31
+    }).catchError((e) {
32
+      print(e);
33
+      Get.defaultDialog(
34
+        title: e.message,
35
+        onConfirm: () => exit(1),
36
+      );
37
+    });
38
+
39
+    // 尝试获取一次人员信息
40
+    getCurrent().then((person) {
41
+      user(person);
42
+    }).catchError((e) {
43
+      print(e);
44
+    });
45
+  }
46
+
47
+}

+ 13
- 0
lib/models/index.dart 查看文件

@@ -0,0 +1,13 @@
1
+
2
+export 'app.dart';
3
+
4
+import 'package:farmer_client/models/app.dart';
5
+import 'package:get/get.dart';
6
+
7
+class AppBindings implements Bindings {
8
+  @override
9
+  void dependencies() {
10
+    Get.put(AppController());
11
+  }
12
+}
13
+

+ 2
- 5
lib/pages/home/index.dart 查看文件

@@ -1,14 +1,11 @@
1 1
 
2
-import 'package:farmer_client/models/User.dart';
3 2
 import 'package:flutter/material.dart';
4
-import 'package:flutter/widgets.dart';
5
-import 'package:get/get.dart';
6 3
 
7
-class Home extends GetView<User> {
4
+class Home extends StatelessWidget {
8 5
   @override
9 6
   Widget build(BuildContext context) {
10 7
     // controller.person
11
-    return Scaffold(body: Text("首页"),);
8
+    return const Scaffold(body: Text("首页"),);
12 9
   }
13 10
 
14 11
 }

+ 5
- 0
lib/pages/login/login.dart 查看文件

@@ -1,5 +1,6 @@
1 1
 import 'dart:async';
2 2
 import 'dart:ffi';
3
+import 'package:farmer_client/models/app.dart';
3 4
 import 'package:fluttertoast/fluttertoast.dart';
4 5
 import 'package:flutter/gestures.dart';
5 6
 import 'package:flutter/material.dart';
@@ -40,6 +41,10 @@ class _RouteLogin extends State<MyRouteLogin> {
40 41
   void initState() {
41 42
     super.initState();
42 43
     //注册协议的手势
44
+
45
+    var location = AppController.t.testInt.value;
46
+    print('--------------');
47
+    print(location);
43 48
   }
44 49
 
45 50
   bool isButtonEnable = true; //按钮状态  是否可点击

+ 4
- 0
lib/services/user.dart 查看文件

@@ -18,3 +18,7 @@ Future getSMSCaptch(String phone) async {
18 18
     );
19 19
   });
20 20
 }
21
+
22
+Future getCurrent() async {
23
+  return request('/person/current');
24
+}

+ 0
- 0
lib/utils/dialog.dart 查看文件


+ 2
- 4
lib/utils/location.dart 查看文件

@@ -1,10 +1,9 @@
1 1
 import 'package:location/location.dart';
2 2
 
3
-Future getLocation() async {
3
+Future requireLocation() async {
4 4
   Location location = Location();
5 5
   bool _serviceEnabled;
6 6
   PermissionStatus _permissionGranted;
7
-  LocationData _locationData;
8 7
 
9 8
   _serviceEnabled = await location.serviceEnabled();
10 9
   if (!_serviceEnabled) {
@@ -22,6 +21,5 @@ Future getLocation() async {
22 21
     }
23 22
   }
24 23
 
25
-  _locationData = await location.getLocation();
26
-  return _locationData;
24
+  return location;
27 25
 }