maintenance_safe_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import 'dart:async';
  2. import 'dart:convert' as convert;
  3. import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart';
  4. import 'package:amap_location_flutter_plugin/amap_location_option.dart';
  5. import 'package:amap_map_fluttify/amap_map_fluttify.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:flutter/foundation.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:keyboard_actions/keyboard_actions.dart';
  10. import 'package:liftmanager/internal/maintenance/maintenance_router.dart';
  11. import 'package:liftmanager/internal/maintenance/model/maintenance_list_entity.dart';
  12. import 'package:liftmanager/net/api_service.dart';
  13. import 'package:liftmanager/res/resources.dart';
  14. import 'package:liftmanager/routers/fluro_navigator.dart';
  15. import 'package:liftmanager/utils/location_helper.dart';
  16. import 'package:liftmanager/utils/theme_utils.dart';
  17. import 'package:liftmanager/utils/toast.dart';
  18. import 'package:liftmanager/widgets/app_bar.dart';
  19. import 'package:liftmanager/widgets/click_item.dart';
  20. import 'package:liftmanager/widgets/my_button.dart';
  21. import 'package:liftmanager/widgets/radio_item.dart';
  22. import 'package:oktoast/oktoast.dart';
  23. import 'package:permission_handler/permission_handler.dart';
  24. class MaintenanceSafePage extends StatefulWidget {
  25. MaintenanceSafePage(this.item);
  26. MaintenanceListItem item;
  27. @override
  28. State<StatefulWidget> createState() {
  29. return MaintenanceSafePageState();
  30. }
  31. }
  32. class MaintenanceSafePageState extends State<MaintenanceSafePage> {
  33. List<String> groupValue = ["", "", "", "", "", ""];
  34. Map<String, Object> _locationResult;
  35. StreamSubscription<Map<String, Object>> _locationListener;
  36. AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
  37. LatLng latLng = LatLng(0, 0);
  38. String currentAddress = "";
  39. @override
  40. void initState() {
  41. super.initState();
  42. // _locationListener = _locationPlugin
  43. // .onLocationChanged()
  44. // .listen((Map<String, Object> result) {
  45. // setState(() {
  46. // _locationPlugin.stopLocation();
  47. // _locationResult = result;
  48. // // address latitude longitude
  49. // _locationResult.forEach((key, value) {
  50. // if (key == 'address') {
  51. // currentAddress = '$value';
  52. // setState(() {});
  53. // } else if (key == 'latitude') {
  54. // latLng.latitude = double.parse('$value');
  55. // setState(() {});
  56. // } else if (key == 'longitude') {
  57. // latLng.longitude = double.parse('$value');
  58. // setState(() {});
  59. // }
  60. // print('key:$key :');
  61. // print('value:$value :');
  62. // });
  63. // });
  64. // });
  65. // WidgetsBinding.instance.addPostFrameCallback((_) async {
  66. // getLocation();
  67. // });
  68. setlatlng();
  69. }
  70. setlatlng() {
  71. var helper = LocationHelper();
  72. helper
  73. .getLocation((String lat, String lng, String cityName, {provinceName}) {
  74. if (lat != null && lng != null) {
  75. helper.stopGettingLocation();
  76. }
  77. latLng = LatLng(double.parse(lat), double.parse(lng));
  78. // setState(() {});
  79. // NavigatorUtils.goBackWithParams(context, "$lng,$lat");
  80. });
  81. }
  82. void _setLocationOption() {
  83. if (null != _locationPlugin) {
  84. AMapLocationOption locationOption = new AMapLocationOption();
  85. ///是否单次定位
  86. locationOption.onceLocation = true;
  87. ///是否需要返回逆地理信息
  88. locationOption.needAddress = true;
  89. ///逆地理信息的语言类型
  90. locationOption.geoLanguage = GeoLanguage.DEFAULT;
  91. ///设置Android端连续定位的定位间隔
  92. locationOption.locationInterval = 20000;
  93. ///设置Android端的定位模式<br>
  94. ///可选值:<br>
  95. ///<li>[AMapLocationMode.Battery_Saving]</li>
  96. ///<li>[AMapLocationMode.Device_Sensors]</li>
  97. ///<li>[AMapLocationMode.Hight_Accuracy]</li>
  98. locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
  99. ///设置iOS端的定位最小更新距离<br>
  100. locationOption.distanceFilter = -1;
  101. ///设置iOS端期望的定位精度
  102. /// 可选值:<br>
  103. /// <li>[DesiredAccuracy.Best] 最高精度</li>
  104. /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
  105. /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
  106. /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
  107. /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
  108. locationOption.desiredAccuracy = DesiredAccuracy.NearestTenMeters;
  109. ///设置iOS端是否允许系统暂停定位
  110. locationOption.pausesLocationUpdatesAutomatically = false;
  111. ///将定位参数设置给定位插件
  112. _locationPlugin.setLocationOption(locationOption);
  113. }
  114. }
  115. @override
  116. void dispose() {
  117. if (null != _locationListener) {
  118. _locationListener.cancel();
  119. }
  120. ///销毁定位
  121. if (null != _locationPlugin) {
  122. _locationPlugin.destroy();
  123. }
  124. super.dispose();
  125. }
  126. ///全选
  127. _allSelected() {
  128. groupValue = ["1", "1", "1", "1", "1", "1"];
  129. setState(() {});
  130. }
  131. ///确认
  132. _stopAndSafe() async {
  133. // if (_location == null) {
  134. // toasts("正在获取定位信息,请稍后");
  135. // return;
  136. // }
  137. int isRegular = 0;
  138. int isRepair = 0;
  139. if (DateTime.parse(widget.item.planDate).isAfter(DateTime.now())) {
  140. isRegular = 1;
  141. }
  142. if (widget.item.status == "-1") {
  143. isRepair = 1;
  144. }
  145. // bool isAllselect = true;
  146. for (var item in groupValue) {
  147. if (item != '1') {
  148. showToast('请勾选安全防护');
  149. return;
  150. }
  151. }
  152. // String address = await _location.address;
  153. // print("${latlng.longitude}");
  154. // print("${latlng.latitude}");
  155. showLoading(context, "正在执行...");
  156. ApiService(context: context).maintenanceRecordAdd(
  157. widget.item.planId,
  158. widget.item.workerId,
  159. widget.item.liftId,
  160. widget.item.liftType,
  161. widget.item.projectId,
  162. widget.item.maintenanceType,
  163. groupValue.toString().substring(1, groupValue.toString().length - 1),
  164. "${latLng.longitude},${latLng.latitude}",
  165. currentAddress,
  166. isRegular,
  167. isRepair, onSuccess: (data) {
  168. dismissLoading(context);
  169. if (data != null) {
  170. widget.item.recordId = data;
  171. String mType = "1";
  172. if (widget.item.maintenanceType == "6" ||
  173. widget.item.maintenanceType == "18") {
  174. mType = "2";
  175. } else if (widget.item.maintenanceType == "12") {
  176. mType = "3";
  177. } else if (widget.item.maintenanceType == "24") {
  178. mType = "4";
  179. }
  180. widget.item.maintenanceType = mType;
  181. String jsonString = convert.jsonEncode(widget.item);
  182. NavigatorUtils.pushResult(context,
  183. "${MaintenanceRouter.maintenanceSubmit}?item=${Uri.encodeComponent(jsonString)}",
  184. (res) {
  185. NavigatorUtils.goBackWithParams(context, true);
  186. print(res);
  187. });
  188. }
  189. }, onError: (code, msg) {
  190. dismissLoading(context);
  191. toasts(msg);
  192. });
  193. }
  194. ///获取位置信息
  195. getLocation() async {
  196. if (await requestPermission()) {
  197. if (null != _locationPlugin) {
  198. ///开始定位之前设置定位参数
  199. _setLocationOption();
  200. _locationPlugin.startLocation();
  201. }
  202. }
  203. }
  204. ///获取定位权限
  205. Future<bool> requestPermission() async {
  206. final permissions = await PermissionHandler()
  207. .requestPermissions([PermissionGroup.location]);
  208. if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
  209. return true;
  210. } else {
  211. toasts('需要定位权限!');
  212. return false;
  213. }
  214. }
  215. @override
  216. Widget build(BuildContext context) {
  217. return Scaffold(
  218. //resizeToAvoidBottomPadding: false,
  219. appBar: const MyAppBar(
  220. centerTitle: "安全确认",
  221. ),
  222. body: SafeArea(
  223. child: Container(
  224. color: ThemeUtils.getBackgroundColor(context),
  225. child: Column(
  226. children: <Widget>[
  227. Expanded(
  228. flex: 1,
  229. child: defaultTargetPlatform == TargetPlatform.iOS
  230. ? FormKeyboardActions(child: _buildBody())
  231. : SingleChildScrollView(child: _buildBody()),
  232. ),
  233. Container(
  234. // color: Colours.bg_color,
  235. child: Padding(
  236. padding: const EdgeInsets.all(16),
  237. child: MyButton(
  238. fontSize: 14,
  239. onPressed: () {
  240. _stopAndSafe();
  241. },
  242. text: "确定",
  243. ),
  244. ))
  245. ],
  246. ),
  247. ),
  248. ));
  249. }
  250. _buildBody() {
  251. return Padding(
  252. padding: EdgeInsets.only(bottom: 30),
  253. child: Column(
  254. crossAxisAlignment: CrossAxisAlignment.start,
  255. children: <Widget>[
  256. ClickItem(
  257. title: "电梯位置",
  258. hintText: "",
  259. content: "${widget.item.devicePosition}"),
  260. Container(
  261. padding: EdgeInsets.only(right: 15),
  262. height: 32.5,
  263. child: Row(
  264. children: <Widget>[
  265. Container(
  266. height: 13,
  267. width: 2,
  268. color: Colours.blue_app_main,
  269. ),
  270. SizedBox(
  271. width: 13,
  272. ),
  273. Expanded(
  274. flex: 1,
  275. child: Text(
  276. "安全防护确认",
  277. style: TextStyle(
  278. fontSize: 12, color: Colours.dark_text_gray),
  279. )),
  280. GestureDetector(
  281. onTap: _allSelected,
  282. child: Text(
  283. "全选",
  284. style: TextStyle(
  285. fontSize: 12, color: Colours.blue_app_main),
  286. )),
  287. ],
  288. )),
  289. RadioItem(
  290. title: "用电安全",
  291. value: "1",
  292. groupValue: groupValue[0],
  293. onTap: () {
  294. groupValue[0] = groupValue[0] == "1" ? "0" : "1";
  295. setState(() {});
  296. }),
  297. RadioItem(
  298. title: "护栏",
  299. value: "1",
  300. groupValue: groupValue[1],
  301. onTap: () {
  302. groupValue[1] = groupValue[1] == "1" ? "0" : "1";
  303. setState(() {});
  304. }),
  305. RadioItem(
  306. title: "手套",
  307. value: "1",
  308. groupValue: groupValue[2],
  309. onTap: () {
  310. groupValue[2] = groupValue[2] == "1" ? "0" : "1";
  311. setState(() {});
  312. }),
  313. RadioItem(
  314. title: "安全帽",
  315. value: "1",
  316. groupValue: groupValue[3],
  317. onTap: () {
  318. groupValue[3] = groupValue[3] == "1" ? "0" : "1";
  319. setState(() {});
  320. }),
  321. RadioItem(
  322. title: "电器防护",
  323. value: "1",
  324. groupValue: groupValue[4],
  325. onTap: () {
  326. groupValue[4] = groupValue[4] == "1" ? "0" : "1";
  327. setState(() {});
  328. }),
  329. RadioItem(
  330. title: "照明",
  331. value: "1",
  332. groupValue: groupValue[5],
  333. onTap: () {
  334. groupValue[5] = groupValue[5] == "1" ? "0" : "1";
  335. setState(() {});
  336. }),
  337. Container(
  338. padding: EdgeInsets.only(left: 15, right: 15),
  339. height: 32.5,
  340. child: Row(
  341. children: <Widget>[
  342. Expanded(
  343. flex: 1,
  344. child: Text(
  345. "请对已安全防护选项进行勾选,确保已做好安全防护工作",
  346. style: TextStyle(
  347. fontSize: 12, color: Colours.blue_app_main),
  348. ))
  349. ],
  350. )),
  351. ],
  352. ),
  353. );
  354. }
  355. }