yearly_upload_page.dart 9.4 KB

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