certificate_upload_page.dart 7.0 KB

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