张延森 3 лет назад
Родитель
Сommit
77d35c11c1

+ 2
- 0
android/app/src/profile/AndroidManifest.xml Просмотреть файл

@@ -4,4 +4,6 @@
4 4
          to allow setting breakpoints, to provide hot reload, etc.
5 5
     -->
6 6
     <uses-permission android:name="android.permission.INTERNET"/>
7
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
8
+    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
7 9
 </manifest>

+ 2
- 2
lib/main.dart Просмотреть файл

@@ -1,8 +1,8 @@
1 1
 import 'dart:async';
2 2
 
3 3
 import 'package:farmer_client/models/Store.dart';
4
-import 'package:farmer_client/pages/home.dart';
5
-import 'package:farmer_client/pages/splash.dart';
4
+import 'package:farmer_client/pages/home/index.dart';
5
+import 'package:farmer_client/pages/splash/splash.dart';
6 6
 import 'package:farmer_client/pages/login/login.dart';
7 7
 import 'package:flutter/material.dart';
8 8
 import 'package:get/get.dart';

lib/pages/home.dart → lib/pages/home/index.dart Просмотреть файл


+ 2
- 2
lib/pages/index.dart Просмотреть файл

@@ -1,4 +1,4 @@
1 1
 
2
-export 'home.dart';
3
-export 'splash.dart';
2
+export 'home/index.dart';
3
+export 'splash/splash.dart';
4 4
 export 'login/login.dart';

+ 0
- 51
lib/pages/splash.dart Просмотреть файл

@@ -1,51 +0,0 @@
1
-
2
-
3
-import 'dart:async';
4
-
5
-import 'package:farmer_client/utils/timer.dart';
6
-import 'package:flutter/widgets.dart';
7
-import 'package:get/get.dart';
8
-
9
-import 'home.dart';
10
-
11
-class SplashScreen extends StatefulWidget {
12
-  @override
13
-  State<StatefulWidget> createState() => SplashScreenState();
14
-}
15
-
16
-class SplashScreenState extends State<SplashScreen> {
17
-
18
-  // 默认 3s
19
-  int _countdown = 3;
20
-  Timer? _timer;
21
-
22
-  @override
23
-  void initState() {
24
-    super.initState();
25
-
26
-    _timer = setInterval((_) {
27
-      if (_countdown <= 1) {
28
-        _timer?.cancel();
29
-        _timer = null;
30
-        Get.off(Home(), routeName: '/');
31
-      }
32
-
33
-      setState(() {
34
-        _countdown -= 1;
35
-      });
36
-    }, 1000);
37
-  }
38
-
39
-  @override
40
-  void dispose() {
41
-    super.dispose();
42
-    _timer?.cancel();
43
-  }
44
-
45
-  @override
46
-  Widget build(BuildContext context) {
47
-    return Text("引导页");
48
-  }
49
-
50
-}
51
-

+ 17
- 0
lib/pages/splash/splash.dart Просмотреть файл

@@ -0,0 +1,17 @@
1
+
2
+import 'package:farmer_client/pages/splash/widgets/countdown.dart';
3
+import 'package:flutter/widgets.dart';
4
+import 'package:get/get.dart';
5
+
6
+import '../home/index.dart';
7
+
8
+class SplashScreen extends StatelessWidget {
9
+  handleOnFinish () {
10
+    Get.off(Home(), routeName: '/');
11
+  }
12
+
13
+  @override
14
+  Widget build(BuildContext context) {
15
+    return countdown(3, handleOnFinish);
16
+  }
17
+}

+ 45
- 0
lib/pages/splash/widgets/countdown.dart Просмотреть файл

@@ -0,0 +1,45 @@
1
+
2
+import 'dart:async';
3
+
4
+import 'package:farmer_client/utils/timer.dart';
5
+import 'package:flutter/widgets.dart';
6
+import 'package:get/get.dart';
7
+
8
+// Widget countdown(void Function() onFinish) {
9
+//   int _countdown = 3;
10
+//   late StateSetter _update;
11
+//   late Timer _timer;
12
+//   _timer = setInterval((_) {
13
+//     if (_countdown <= 1) {
14
+//       _timer.cancel();
15
+//       onFinish();
16
+//       return;
17
+//     }
18
+//
19
+//     _update((){
20
+//       _countdown -= 1;
21
+//     });
22
+//   }, 1000);
23
+//
24
+//   return StatefulBuilder(builder: (context, setState) {
25
+//     _update = setState;
26
+//     return Text(_countdown.toString());
27
+//   });
28
+// }
29
+
30
+Widget countdown(int sec, void Function() onFinish) {
31
+  var _countdown = Rx<int>(sec);
32
+
33
+  late Timer _timer;
34
+  _timer = setInterval((_) {
35
+    if (_countdown.value <= 1) {
36
+      _timer.cancel();
37
+      onFinish();
38
+      return;
39
+    }
40
+
41
+    _countdown.value -= 1;
42
+  }, 1000);
43
+
44
+  return Obx(() =>Text(_countdown.value.toString()));
45
+}

+ 27
- 0
lib/utils/location.dart Просмотреть файл

@@ -0,0 +1,27 @@
1
+import 'package:location/location.dart';
2
+
3
+Future getLocation() async {
4
+  Location location = Location();
5
+  bool _serviceEnabled;
6
+  PermissionStatus _permissionGranted;
7
+  LocationData _locationData;
8
+
9
+  _serviceEnabled = await location.serviceEnabled();
10
+  if (!_serviceEnabled) {
11
+    _serviceEnabled = await location.requestService();
12
+    if (!_serviceEnabled) {
13
+      throw Exception("请开启系统定位功能");
14
+    }
15
+  }
16
+
17
+  _permissionGranted = await location.hasPermission();
18
+  if (_permissionGranted == PermissionStatus.denied) {
19
+    _permissionGranted = await location.requestPermission();
20
+    if (_permissionGranted != PermissionStatus.granted) {
21
+      throw Exception("请设置允许当前程序定位功能");
22
+    }
23
+  }
24
+
25
+  _locationData = await location.getLocation();
26
+  return _locationData;
27
+}

+ 21
- 0
pubspec.lock Просмотреть файл

@@ -233,6 +233,27 @@ packages:
233 233
       url: "https://pub.flutter-io.cn"
234 234
     source: hosted
235 235
     version: "1.0.1"
236
+  location:
237
+    dependency: "direct main"
238
+    description:
239
+      name: location
240
+      url: "https://pub.flutter-io.cn"
241
+    source: hosted
242
+    version: "4.3.0"
243
+  location_platform_interface:
244
+    dependency: transitive
245
+    description:
246
+      name: location_platform_interface
247
+      url: "https://pub.flutter-io.cn"
248
+    source: hosted
249
+    version: "2.3.0"
250
+  location_web:
251
+    dependency: transitive
252
+    description:
253
+      name: location_web
254
+      url: "https://pub.flutter-io.cn"
255
+    source: hosted
256
+    version: "3.1.1"
236 257
   logging:
237 258
     dependency: transitive
238 259
     description:

+ 1
- 0
pubspec.yaml Просмотреть файл

@@ -39,6 +39,7 @@ dependencies:
39 39
   dio: ^4.0.6
40 40
   flutter_screenutil: ^5.3.1
41 41
   get_storage: ^2.0.3
42
+  location: ^4.3.0
42 43
 
43 44
 dev_dependencies:
44 45
   flutter_test: