certificate_upload_page.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:keyboard_actions/keyboard_actions.dart';
  8. import 'package:liftmanager/internal/account/model/certificate_item.dart';
  9. import 'package:liftmanager/net/api_service.dart';
  10. import 'package:liftmanager/res/resources.dart';
  11. import 'package:liftmanager/routers/fluro_navigator.dart';
  12. import 'package:liftmanager/utils/theme_utils.dart';
  13. import 'package:liftmanager/utils/toast.dart';
  14. import 'package:liftmanager/widgets/app_bar.dart';
  15. import 'package:liftmanager/widgets/click_item.dart';
  16. import 'package:liftmanager/widgets/selected_image.dart';
  17. import 'package:liftmanager/widgets/text_field_item.dart';
  18. class CertificateUploadPage extends StatefulWidget {
  19. @override
  20. State<StatefulWidget> createState() {
  21. return CertificateUploadPageState();
  22. }
  23. }
  24. class CertificateUploadPageState extends State<CertificateUploadPage> {
  25. final FocusNode _nodeText1 = FocusNode();
  26. final FocusNode _nodeText2 = FocusNode();
  27. final FocusNode _nodeText3 = FocusNode();
  28. CertificateItem item = CertificateItem();
  29. List<File> images = [];
  30. @override
  31. void initState() {
  32. super.initState();
  33. }
  34. uploadCertificate() {
  35. if (item.code.length == 0 ||
  36. item.issuanceAgency.length == 0 ||
  37. item.expirationDate.length == 0) {
  38. toasts("请填写完整信息");
  39. return;
  40. }
  41. if (images.length != 2) {
  42. toasts("请上传操作证");
  43. return;
  44. }
  45. showLoading(context, "正在提交...");
  46. ApiService(context: context).uploadMore(images, onSuccess: (imgs) {
  47. ApiService(context: context).liftCertificateAdd(
  48. item.code,
  49. item.issuanceAgency,
  50. item.expirationDate,
  51. item.certificateType,
  52. imgs[0],
  53. imgs[1], onSuccess: (res) {
  54. dismissLoading(context);
  55. showAlert(context, "提示", "保存成功", "确定", () {
  56. NavigatorUtils.goBack(context);
  57. NavigatorUtils.goBackWithParams(context, true);
  58. });
  59. }, onError: (code, msg) {
  60. dismissLoading(context);
  61. toasts(msg);
  62. });
  63. });
  64. }
  65. /// 选择时间
  66. Future<void> _selectTime(Function callback) async {
  67. DatePicker.showDatePicker(context,
  68. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  69. callback("${date.toString().split(".")[0]}");
  70. }, currentTime: DateTime.now(), locale: LocaleType.zh);
  71. }
  72. ///选择图片
  73. void selectPicker(int index) {
  74. showDialog(
  75. context: context,
  76. builder: (BuildContext context) {
  77. return SimpleDialog(
  78. title: Text("选择方式"),
  79. children: ["拍照", '从手机相册选择'].map((String value) {
  80. print("$value");
  81. return SimpleDialogOption(
  82. child: Text(
  83. "${value}",
  84. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  85. ),
  86. onPressed: () {
  87. _getImage(value == '拍照' ? 1 : 0, index);
  88. Navigator.of(context).pop();
  89. },
  90. );
  91. }).toList());
  92. });
  93. }
  94. void _getImage(int key, int index) async {
  95. try {
  96. var _imageFile = await ImagePicker.pickImage(
  97. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  98. maxWidth: 800,
  99. imageQuality: 95);
  100. if (_imageFile != null) {
  101. if (images.length > index) {
  102. images[index] = _imageFile;
  103. } else {
  104. images.add(_imageFile);
  105. }
  106. setState(() {});
  107. }
  108. } catch (e) {
  109. toasts("没有权限,无法打开相册!");
  110. }
  111. }
  112. @override
  113. Widget build(BuildContext context) {
  114. return Scaffold(
  115. appBar: MyAppBar(
  116. centerTitle: "操作证上传",
  117. actions: <Widget>[
  118. FlatButton(
  119. child: Text("确定"),
  120. textColor: Colours.text,
  121. highlightColor: Colors.transparent,
  122. onPressed: () {
  123. uploadCertificate();
  124. },
  125. )
  126. ],
  127. ),
  128. body: SafeArea(
  129. child: Column(
  130. children: <Widget>[
  131. Expanded(
  132. flex: 1,
  133. child: defaultTargetPlatform == TargetPlatform.iOS
  134. ? FormKeyboardActions(child: _buildBody())
  135. : SingleChildScrollView(child: _buildBody()),
  136. )
  137. ],
  138. ),
  139. ),
  140. );
  141. }
  142. _buildBody() {
  143. return Column(
  144. crossAxisAlignment: CrossAxisAlignment.start,
  145. children: <Widget>[
  146. TextFieldItem(
  147. title: "操作证编号",
  148. content: "${item.code}",
  149. controller: TextEditingController(),
  150. hintText: "请填写操作证编号",
  151. focusNode: _nodeText1,
  152. onChanged: (res) {
  153. item.code = res;
  154. // setState(() {});
  155. },
  156. ),
  157. ClickItem(
  158. title: "证件有效期",
  159. content: "${item.expirationDate}",
  160. hintText: "请选择证件有效期",
  161. onTap: () {
  162. _selectTime((String time) {
  163. setState(() {
  164. item.expirationDate = time.split(" ")[0];
  165. });
  166. });
  167. },
  168. ),
  169. TextFieldItem(
  170. title: "发证单位",
  171. content: "${item.issuanceAgency}",
  172. controller: TextEditingController(),
  173. hintText: "请填写发证单位",
  174. focusNode: _nodeText2,
  175. onChanged: (res) {
  176. item.issuanceAgency = res;
  177. },
  178. ),
  179. TextFieldItem(
  180. title: "证件类型",
  181. content: "${item.certificateType}",
  182. controller: TextEditingController(),
  183. hintText: "请填写证件类型(如:T1)",
  184. focusNode: _nodeText3,
  185. onChanged: (res) {
  186. item.certificateType = res;
  187. },
  188. ),
  189. ClickItem(
  190. title: "操作证图片",
  191. content: "",
  192. hideDiv: true,
  193. ),
  194. Container(
  195. color: ThemeUtils.getTabsBg(context),
  196. child: GridView.builder(
  197. shrinkWrap: true,
  198. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  199. physics: NeverScrollableScrollPhysics(),
  200. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  201. crossAxisCount: 3, childAspectRatio: 1.18),
  202. itemCount: images.length >= 2 ? 2 : images.length + 1,
  203. itemBuilder: (_, index) {
  204. return Stack(
  205. children: <Widget>[
  206. Center(
  207. child: SelectedImage(
  208. image: index < images.length ? images[index] : null,
  209. onTap: () {
  210. selectPicker(index);
  211. }),
  212. )
  213. ],
  214. );
  215. },
  216. )),
  217. ],
  218. );
  219. }
  220. }