yearly_upload_page.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  5. import 'package:image_picker/image_picker.dart';
  6. import 'package:liftmanager/net/api_service.dart';
  7. import 'package:liftmanager/res/resources.dart';
  8. import 'package:liftmanager/routers/fluro_navigator.dart';
  9. import 'package:liftmanager/utils/theme_utils.dart';
  10. import 'package:liftmanager/utils/toast.dart';
  11. import 'package:liftmanager/widgets/app_bar.dart';
  12. import 'package:liftmanager/widgets/click_item.dart';
  13. import 'package:liftmanager/widgets/my_button.dart';
  14. import 'package:liftmanager/widgets/selected_image.dart';
  15. import 'package:oktoast/oktoast.dart';
  16. class YearlyUploadPage extends StatefulWidget {
  17. YearlyUploadPage(this.type, this.id);
  18. int type; //0自检报告 1检验报告 2上传年检报告和合格证
  19. String id;
  20. @override
  21. State<StatefulWidget> createState() {
  22. return YearlyUploadPageState();
  23. }
  24. }
  25. class YearlyUploadPageState extends State<YearlyUploadPage> {
  26. String _stopTime = "";
  27. File _imageFile;
  28. File _imageFilezj2;
  29. File _imageFilezj3;
  30. File _imageFile2;
  31. List<String> listStr = ["自检", "检验", "年检"];
  32. /// 选择时间
  33. Future<void> _selectTime() async {
  34. DatePicker.showDateTimePicker(context,
  35. showTitleActions: true,
  36. onChanged: (date) {},
  37. onConfirm: (date) {
  38. _stopTime = "${date.toString().split(" ")[0]}";
  39. setState(() {});
  40. },
  41. currentTime: DateTime.now(),
  42. locale: LocaleType.zh);
  43. }
  44. ///保存自检报告
  45. _saveSelfCheck() {
  46. if (_stopTime.length == 0) {
  47. toasts("请选择${listStr[widget.type]}时间");
  48. return;
  49. }
  50. if (_imageFile == null) {
  51. toasts("请上传${listStr[widget.type]}图片");
  52. return;
  53. }
  54. List<File> list = [_imageFile];
  55. if(_imageFilezj2 != null){
  56. list.add(_imageFilezj2);
  57. }
  58. if(_imageFilezj3 != null){
  59. list.add(_imageFilezj3);
  60. }
  61. showLoading(context, "正在提交...");
  62. ApiService(context: context).uploadMore(
  63. list, onSuccess: (List<String> imgUrls) {
  64. ApiService(context: context).yearlySelfCheck(widget.id, _stopTime, imgUrls.toString().substring(1,imgUrls.toString().length-1),
  65. onSuccess: (data) {
  66. dismissLoading(context);
  67. showAlert(context, "提示", "提交成功", "确定", () {
  68. NavigatorUtils.goBack(context);
  69. NavigatorUtils.goBackWithParams(
  70. context, {"time": _stopTime, "img": imgUrls.toString().substring(1,imgUrls.toString().length-1)});
  71. });
  72. }, onError: (code, msg) {
  73. toasts(msg);
  74. dismissLoading(context);
  75. });
  76. }, onError: (code, msg) {
  77. dismissLoading(context);
  78. toasts(msg);
  79. });
  80. }
  81. ///检验结果
  82. _saveCheck() {
  83. if (_stopTime.length == 0) {
  84. toasts("请选择${listStr[widget.type]}时间");
  85. return;
  86. }
  87. if (_imageFile == null) {
  88. toasts("请上传${listStr[widget.type]}图片");
  89. return;
  90. }
  91. showLoading(context, "正在提交...");
  92. ApiService(context: context).upload(
  93. _imageFile.path, onSuccess: (String imgUrl) {
  94. ApiService(context: context).yearlyCheck(
  95. widget.id, _stopTime, imgUrl, onSuccess: (data) {
  96. dismissLoading(context);
  97. showAlert(context, "提示", "提交成功", "确定", () {
  98. NavigatorUtils.goBack(context);
  99. NavigatorUtils.goBackWithParams(
  100. context, {"time": _stopTime, "img": imgUrl});
  101. });
  102. }, onError: (code, msg) {
  103. toasts(msg);
  104. dismissLoading(context);
  105. });
  106. }, onError: (code, msg) {
  107. dismissLoading(context);
  108. toasts(msg);
  109. });
  110. }
  111. ///合格证
  112. _saveFinish() {
  113. if (_imageFile == null) {
  114. toasts("请上传${listStr[widget.type]}图片");
  115. return;
  116. }
  117. if (_imageFile2 == null) {
  118. toasts("请上传合格证图片");
  119. return;
  120. }
  121. showLoading(context, "正在提交...");
  122. ApiService(context: context).uploadMore([_imageFile, _imageFile2],
  123. onSuccess: (List<String> imgList) {
  124. if (imgList.length != 2) {
  125. dismissLoading(context);
  126. toasts("上传图片失败");
  127. return;
  128. }
  129. ApiService(context: context).yearlyFinish(
  130. widget.id, imgList[0], imgList[1],
  131. onSuccess: (data) {
  132. dismissLoading(context);
  133. showAlert(context, "提示", "提交成功", "确定", () {
  134. NavigatorUtils.goBack(context);
  135. NavigatorUtils.goBackWithParams(
  136. context, {"img1": imgList[0], "img2": imgList[1]});
  137. });
  138. }, onError: (code, msg) {
  139. showToast(msg);
  140. dismissLoading(context);
  141. });
  142. }, onError: (code, msg) {
  143. dismissLoading(context);
  144. toasts(msg);
  145. });
  146. }
  147. ///选择图片
  148. void selectPicker(int index) {
  149. showDialog(
  150. context: context,
  151. builder: (BuildContext context) {
  152. return SimpleDialog(
  153. title: Text("选择方式"),
  154. children: ["拍照", '从手机相册选择'].map((String value) {
  155. print("$value");
  156. return SimpleDialogOption(
  157. child: Text(
  158. "${value}",
  159. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  160. ),
  161. onPressed: () {
  162. _getImage(value == '拍照' ? 1 : 0, index);
  163. Navigator.of(context).pop();
  164. },
  165. );
  166. }).toList());
  167. });
  168. }
  169. void _getImage(int key, int index) async {
  170. try {
  171. var file = await ImagePicker.pickImage(
  172. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  173. maxWidth: 800,
  174. imageQuality: 95);
  175. if (index == 1) {
  176. _imageFile = file;
  177. } else if (index == 22) {
  178. _imageFilezj2 = file;
  179. } else if (index == 33) {
  180. _imageFilezj3 = file;
  181. } else {
  182. _imageFile2 = file;
  183. }
  184. setState(() {});
  185. } catch (e) {
  186. toasts("没有权限,无法打开相册!");
  187. }
  188. }
  189. @override
  190. Widget build(BuildContext context) {
  191. print(widget.type);
  192. print(_imageFile);
  193. return Scaffold(
  194. backgroundColor: ThemeUtils.getTabsBg(context),
  195. appBar: MyAppBar(
  196. centerTitle: "上传${listStr[widget.type]}报告",
  197. ),
  198. body: Column(
  199. crossAxisAlignment: CrossAxisAlignment.start,
  200. children: <Widget>[
  201. Offstage(
  202. offstage: widget.type == 2,
  203. child: ClickItem(
  204. title: "${listStr[widget.type]}时间",
  205. hintText: "请选择${listStr[widget.type]}时间",
  206. content: "${_stopTime}",
  207. onTap: () {
  208. _selectTime();
  209. },
  210. ),
  211. ),
  212. Container(
  213. padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
  214. child: Text("${listStr[widget.type]}报告"),
  215. ),
  216. Row(
  217. children: <Widget>[
  218. Container(
  219. padding: EdgeInsets.all(15),
  220. child: SelectedImage(
  221. image: _imageFile,
  222. onTap: () {
  223. selectPicker(1);
  224. })
  225. ),
  226. Offstage(
  227. offstage: (widget.type != 0 || _imageFile ==null),
  228. child: Container(
  229. padding: EdgeInsets.all(15),
  230. child: SelectedImage(
  231. image: _imageFilezj2,
  232. onTap: () {
  233. selectPicker(22);
  234. })
  235. ),
  236. ),
  237. Offstage(
  238. offstage: widget.type != 0 || _imageFilezj2 ==null,
  239. child: Container(
  240. padding: EdgeInsets.all(15),
  241. child: SelectedImage(
  242. image: _imageFilezj3,
  243. onTap: () {
  244. selectPicker(33);
  245. })
  246. ),
  247. ),
  248. ],
  249. ),
  250. Offstage(
  251. offstage: widget.type != 2,
  252. child: Column(
  253. crossAxisAlignment: CrossAxisAlignment.start,
  254. children: <Widget>[
  255. Container(
  256. padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
  257. child: Text("合格证"),
  258. ),
  259. Container(
  260. padding: EdgeInsets.all(15),
  261. color: Colors.white,
  262. child: SelectedImage(
  263. image: _imageFile2,
  264. onTap: () {
  265. selectPicker(2);
  266. }),
  267. ),
  268. ],
  269. ),
  270. ),
  271. Container(
  272. child: Padding(
  273. padding: const EdgeInsets.all(16),
  274. child: MyButton(
  275. fontSize: 14,
  276. onPressed: () {
  277. print("${widget.type}");
  278. if (widget.type == 0) {
  279. _saveSelfCheck();
  280. } else if (widget.type == 1) {
  281. _saveCheck();
  282. } else {
  283. _saveFinish();
  284. }
  285. },
  286. text: "确定",
  287. ),
  288. ))
  289. ],
  290. ),
  291. );
  292. }
  293. }