12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import 'dart:io';
  2. import 'package:farmer_client/models/entities/person.dart';
  3. import 'package:get/get.dart';
  4. import 'package:location/location.dart';
  5. import '../services/user.dart';
  6. import '../utils/location.dart';
  7. class AppController extends GetxController {
  8. // 有了这句, 可以直接 AppController.t 调用
  9. static AppController t = Get.find();
  10. final user = Rx<Person>(Person());
  11. final location = Rxn<LocationData>();
  12. final testInt = 1.obs;
  13. get locationStr {
  14. if (null == location.value) return null;
  15. return location.value!.longitude.toString() + "," + location.value!.latitude.toString();
  16. }
  17. @override
  18. void onInit() {
  19. super.onInit();
  20. // 尝试获取 location
  21. requireLocation().then((loc) async {
  22. var _data = await loc.getLocation();
  23. location(_data);
  24. // 监听位置变化
  25. loc.onLocationChanged.listen((LocationData currentLocation) {
  26. location(currentLocation);
  27. });
  28. }).catchError((e) {
  29. print(e);
  30. var message = e.message.toString().contains('CANCELED')
  31. ? '您取消了, 确定后将退出程序'
  32. : e.message;
  33. // Get.dialog();
  34. // Get.defaultDialog(
  35. // title: '提示',
  36. // middleText: message,
  37. // radius: 8,
  38. // textConfirm: '确定',
  39. // onConfirm: () => exit(1),
  40. // );
  41. });
  42. // // 尝试获取一次人员信息
  43. // getCurrent().then((person) {
  44. // user(person);
  45. // }).catchError((e) {
  46. // print(e);
  47. // });
  48. }
  49. }