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