123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 '../services/user.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. get locationStr {
  16. if (null == location) return null;
  17. double longitude = location.value!['longitude'] as double;
  18. double latitude = location.value!['latitude'] as double;
  19. return longitude.toString() + "," + latitude.toString();
  20. }
  21. void onLocationChange (Map<String, Object> result) {
  22. location(result);
  23. }
  24. @override
  25. void onInit() {
  26. super.onInit();
  27. // 尝试获取 location
  28. requireLocation(onLocationChange).then((loc) {
  29. _location = loc;
  30. }).catchError((e) {
  31. print(e);
  32. showAlert(
  33. title: '获取定位失败',
  34. message: e.message,
  35. onConfirm: () => exit(1),
  36. );
  37. });
  38. // 尝试获取一次人员信息
  39. getCurrent().then((person) {
  40. print('9personpersonpersonperson+$person');
  41. user(Person.fromJson(person));
  42. }).catchError((e) {
  43. print(e);
  44. });
  45. }
  46. @override
  47. void onClose() {
  48. if (null != _location) {
  49. _location?.destroy();
  50. }
  51. }
  52. }