maintenance_safe_page.dart 11 KB

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