maintenance_safe_page.dart 12 KB

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