track_controller.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_map/flutter_map.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get.dart';
  5. import 'package:get/get_core/src/get_main.dart';
  6. import 'package:injectable/injectable.dart';
  7. import 'package:location/base/base_controller.dart';
  8. import 'package:location/data/bean/track_days.dart';
  9. import 'package:location/data/consts/constants.dart';
  10. import 'package:location/data/repositories/account_repository.dart';
  11. import 'package:location/data/repositories/friends_repository.dart';
  12. import 'package:location/data/repositories/track_repository.dart';
  13. import 'package:location/handler/error_handler.dart';
  14. import 'package:location/resource/string.gen.dart';
  15. import 'package:permission_handler/permission_handler.dart';
  16. import 'package:sliding_sheet2/sliding_sheet2.dart';
  17. import '../../data/bean/user_info.dart';
  18. import '../../dialog/common_confirm_dialog_impl.dart';
  19. import '../../dialog/location_permission_dialog.dart';
  20. import '../../sdk/map/map_helper.dart';
  21. import '../../utils/permission_util.dart';
  22. import '../../utils/toast_util.dart';
  23. @injectable
  24. class TrackController extends BaseController
  25. with GetSingleTickerProviderStateMixin {
  26. final Rxn<UserInfo> _userInfo = Rxn<UserInfo>();
  27. UserInfo? get userInfo => _userInfo.value;
  28. final MapController mapController = MapController();
  29. SheetController sheetController = SheetController();
  30. final RxList<TrackDays> daysList = RxList<TrackDays>();
  31. final Rxn<TabController> _tabController = Rxn<TabController>();
  32. TabController? get tabController => _tabController.value;
  33. final Rxn<TrackDays> currentTrackDay = Rxn();
  34. final RxDouble _sheetProgress = 0.0.obs;
  35. double get sheetProgress => _sheetProgress.value;
  36. final mapPadding =
  37. MapPadding(left: 50.w, top: 100.w, right: 50.w, bottom: Get.height / 2);
  38. final selectPadding =
  39. MapPadding(left: 80.w, top: 150.w, right: 80.w, bottom: Get.height / 2);
  40. final RxDouble trackBottomHeight = RxDouble(330.w);
  41. final TrackRepository trackRepository;
  42. final FriendsRepository friendsRepository;
  43. final AccountRepository accountRepository;
  44. TrackController(
  45. this.trackRepository, this.friendsRepository, this.accountRepository);
  46. @override
  47. void onInit() {
  48. final param = Get.arguments;
  49. if (param is UserInfo) {
  50. _userInfo.value = param;
  51. }
  52. _onRequestTrackDateList();
  53. }
  54. @override
  55. void onReady() {
  56. super.onReady();
  57. sheetController.expand();
  58. }
  59. void _onRequestTrackDateList() {
  60. trackRepository.getLocationTrackDays().then((list) {
  61. daysList.assignAll(list);
  62. _createTabController();
  63. }).catchError((error) {
  64. ErrorHandler.toastError(error);
  65. });
  66. }
  67. void _createTabController() {
  68. tabController?.dispose();
  69. final tab = TabController(
  70. length: daysList.length,
  71. vsync: this,
  72. );
  73. _tabController.value = tab;
  74. tab.addListener(() {
  75. if (tab.indexIsChanging == false) {
  76. //控制请求当前页数据或者数据已有时跟换数据
  77. mapController.clear();
  78. currentTrackDay.value = daysList[tab.index];
  79. }
  80. });
  81. Future.delayed(Duration(milliseconds: 100), () {
  82. currentTrackDay.value = daysList.isNotEmpty ? daysList[0] : null;
  83. });
  84. }
  85. ///显示轨迹以及标记点
  86. void showMapTrack(List<LatLng> points, List<Marker> markers) {
  87. mapController.clear();
  88. //画折线
  89. if (points.length > 1) {
  90. mapController.addPolyline(
  91. Polyline(lineId: Constants.traceNormalLineId, points: points),
  92. mapPadding: mapPadding);
  93. }
  94. //画标记点
  95. if (markers.isNotEmpty) {
  96. mapController.updateOrAddMarkers(markers);
  97. }
  98. }
  99. void showSelectMarker(List<LatLng> points, Marker selectMarker) {
  100. _clearSelectMapMarker();
  101. mapController.updateOrAddMarker(selectMarker);
  102. mapController.moveToSuitableLocation(points, mapPadding: selectPadding);
  103. }
  104. void back() {
  105. Get.back();
  106. }
  107. void showMovingTrack(List<LatLng> movingPoints) {
  108. _clearSelectMapMarker();
  109. mapController.addPolyline(
  110. Polyline(
  111. lineId: Constants.traceSelectLineId,
  112. points: movingPoints,
  113. lineType: PolylineType.selected),
  114. mapPadding: mapPadding);
  115. }
  116. void showTrackError(List<LatLng> errorPoints, Marker errorMarker) {
  117. _clearSelectMapMarker();
  118. mapController.updateOrAddMarker(errorMarker);
  119. mapController.addPolyline(
  120. Polyline(
  121. lineId: Constants.traceSelectLineId,
  122. points: errorPoints,
  123. lineType: PolylineType.error),
  124. mapPadding: mapPadding);
  125. }
  126. void _clearSelectMapMarker() {
  127. mapController.removeMarker(Constants.mineLocationId);
  128. mapController.removePolyline(Constants.traceSelectLineId);
  129. mapController.removeMarker(Constants.tracePopupId);
  130. mapController.removeMarker(Constants.traceErrorId);
  131. }
  132. setSheetProgress(double progress) {
  133. _sheetProgress.value = progress;
  134. if (progress >= 1) {
  135. trackBottomHeight.value = 0;
  136. } else if (progress <= 0.45) {
  137. trackBottomHeight.value = 330.w;
  138. } else {
  139. double percent = (1 - progress) / (1 - 0.45);
  140. trackBottomHeight.value = 330.w * percent;
  141. }
  142. }
  143. void onCurrentLocationClick() async {
  144. //权限检查
  145. bool isGranted = await PermissionUtil.checkLocationPermission();
  146. if (!isGranted) {
  147. LocationPermissionDialog.show(onNextStep: _requestLocationPermission);
  148. } else {
  149. _updateCurrentLocation();
  150. }
  151. }
  152. void _updateCurrentLocation() {
  153. var lastLocation = MapHelper.getLastLocation();
  154. if (lastLocation == null) {
  155. locationListener(MapLocation location) {
  156. MapHelper.removeLocationListener(locationListener);
  157. showMineLocationMarker(location);
  158. }
  159. MapHelper.addLocationListener(locationListener);
  160. } else {
  161. showMineLocationMarker(lastLocation);
  162. }
  163. }
  164. void showMineLocationMarker(MapLocation location) {
  165. _clearSelectMapMarker();
  166. Marker mineMarker = Marker(
  167. id: Constants.mineLocationId,
  168. latitude: location.latitude,
  169. longitude: location.longitude,
  170. isSelected: false,
  171. customAvatarUrl: accountRepository.mineUserInfo.value.avatar,
  172. markerName: StringName.locationMine,
  173. markerType: MarkerType.mine);
  174. mapController.updateOrAddMarker(mineMarker);
  175. animateCamera(latitude: location.latitude, longitude: location.longitude);
  176. }
  177. void animateCamera({required double? latitude, required double? longitude}) {
  178. mapController.animateCamera(
  179. CameraPosition(longitude: longitude, latitude: latitude, zoom: 18));
  180. }
  181. void _requestLocationPermission() async {
  182. bool isGranted = await PermissionUtil.requestLocationPermission();
  183. if (isGranted) {
  184. _showLocationAlways();
  185. _updateCurrentLocation();
  186. } else {
  187. permissionRefuseDialog(settingClick: () {
  188. openAppSettings();
  189. });
  190. ToastUtil.show(StringName.permissionRequestFail);
  191. }
  192. }
  193. void _showLocationAlways() async {
  194. bool isGranted = await PermissionUtil.checkShowLocationAlways();
  195. if (!isGranted) {
  196. LocationAlwaysPermissionDialog.show(onNextStep: () async {
  197. isGranted = await PermissionUtil.requestShowLocationAlways();
  198. if (isGranted) {
  199. _updateCurrentLocation();
  200. }
  201. });
  202. }
  203. }
  204. @override
  205. void onClose() {
  206. super.onClose();
  207. tabController?.dispose();
  208. }
  209. }