track_controller.dart 7.3 KB

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