app.dart 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:amap_flutter_location/amap_flutter_location.dart';
  4. import 'package:farmer_client/models/entities/person.dart';
  5. import 'package:farmer_client/widgets/Modal.dart';
  6. import 'package:get/get.dart';
  7. import '../utils/location.dart';
  8. class AppController extends GetxController {
  9. // 有了这句, 可以直接 AppController.t 调用
  10. static AppController t = Get.find();
  11. final user = Rx<Person>(Person());
  12. final location = Rxn<Map<String, Object>>();
  13. final testInt = 1.obs;
  14. AMapFlutterLocation? _location;
  15. StreamSubscription<Map<String, Object>>? _locationListener;
  16. get locationStr {
  17. if (null == location.value) return null;
  18. double longitude = location.value!['longitude'] as double;
  19. double latitude = location.value!['latitude'] as double;
  20. return longitude.toString() + "," + latitude.toString();
  21. }
  22. @override
  23. void onInit() {
  24. super.onInit();
  25. // 尝试获取 location
  26. requireLocation().then((loc) async {
  27. _location = loc;
  28. // 监听位置变化
  29. _locationListener = loc.onLocationChanged().listen((Map<String, Object> result) {
  30. location(result);
  31. });
  32. loc.startLocation();
  33. }).catchError((e) {
  34. print(e);
  35. showAlert(
  36. title: '获取定位失败',
  37. message: e.message,
  38. onConfirm: () => exit(1),
  39. );
  40. });
  41. // // 尝试获取一次人员信息
  42. // getCurrent().then((person) {
  43. // user(person);
  44. // }).catchError((e) {
  45. // print(e);
  46. // });
  47. }
  48. @override
  49. void onClose() {
  50. if (null != _locationListener) {
  51. _locationListener?.cancel();
  52. }
  53. if (null != _location) {
  54. _location?.destroy();
  55. }
  56. }
  57. }