certificate_upload_page.dart 7.1 KB

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