app.dart 1.5KB

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