submit_page.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import 'dart:io';
  2. import 'package:file_picker/file_picker.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_tracker/dio/track_dao.dart';
  5. import 'package:flutter_tracker/model/config.dart';
  6. import 'package:flutter_tracker/model/submit_model.dart';
  7. import 'package:flutter_tracker/utils/app_util.dart';
  8. import 'package:shared_preferences/shared_preferences.dart';
  9. /// Description:
  10. /// Time : 2021年12月03日 Friday
  11. /// Author : liuyuqi.gov@msncn
  12. class SubmitPage extends StatefulWidget {
  13. const SubmitPage({Key key}) : super(key: key);
  14. @override
  15. _SubmitPageState createState() => _SubmitPageState();
  16. }
  17. class _SubmitPageState extends State<SubmitPage> {
  18. final TextEditingController _controllerUsn = TextEditingController();
  19. final TextEditingController _controllerTel = TextEditingController();
  20. final TextEditingController _controllerAddress = TextEditingController();
  21. final GlobalKey _formKey = GlobalKey<FormState>();
  22. String _userName = "";
  23. String _tel = "";
  24. String _addresds = "";
  25. int _heathStatus = 1;
  26. int _submitid = 0;
  27. String _filePath;
  28. Size get _size => MediaQuery.of(context).size;
  29. @override
  30. Widget build(BuildContext context) {
  31. return Scaffold(
  32. appBar: AppBar(
  33. title: Text("疫情上报"),
  34. ),
  35. body: SingleChildScrollView(
  36. child: Padding(
  37. padding: const EdgeInsets.only(left: 8.0, right: 8),
  38. child: Column(
  39. children: [
  40. TextFormField(
  41. autofocus: true,
  42. controller: _controllerUsn,
  43. decoration: const InputDecoration(
  44. labelText: "姓名:",
  45. hintText: "请输入真实姓名",
  46. icon: Icon(Icons.person)),
  47. // 校验用户名
  48. validator: (v) {
  49. return v.trim().length > 0 ? null : "姓名不能为空";
  50. },
  51. onChanged: (inputStr) {
  52. _userName = inputStr;
  53. },
  54. ),
  55. TextFormField(
  56. controller: _controllerTel,
  57. decoration: const InputDecoration(
  58. labelText: "电话:",
  59. hintText: "联系电话",
  60. icon: Icon(Icons.phone)),
  61. validator: (v) {
  62. return v.trim().length > 11 ? null : "手机号错误";
  63. },
  64. onChanged: (inputStr) {
  65. _tel = inputStr;
  66. },
  67. ),
  68. TextFormField(
  69. controller: _controllerAddress,
  70. decoration: const InputDecoration(
  71. labelText: "住址:",
  72. hintText: "请输入家庭住址",
  73. icon: Icon(Icons.house)),
  74. validator: (v) {
  75. return v.trim().length > 0 ? null : "请输入地址";
  76. },
  77. onChanged: (inputStr) {
  78. _addresds = inputStr;
  79. },
  80. ),
  81. SizedBox(
  82. height: 10,
  83. ),
  84. Align(
  85. child: Text(
  86. "健康状态:",
  87. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  88. ),
  89. alignment: Alignment.centerLeft,
  90. ),
  91. RadioListTile(
  92. title: Text("阴性"),
  93. value: 1,
  94. groupValue: _heathStatus,
  95. onChanged: (value) {
  96. setState(() {
  97. _heathStatus = value;
  98. });
  99. },
  100. ),
  101. RadioListTile(
  102. title: Text("阳性"),
  103. value: 2,
  104. groupValue: _heathStatus,
  105. onChanged: (value) {
  106. setState(() {
  107. _heathStatus = value;
  108. });
  109. },
  110. ),
  111. SizedBox(
  112. height: 10,
  113. ),
  114. Align(
  115. child: Text(
  116. "核酸检测:",
  117. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  118. ),
  119. alignment: Alignment.centerLeft,
  120. ),
  121. Text("请拍照上传清晰的核酸证明图片"),
  122. InkWell(
  123. onTap: () {
  124. chooseImg();
  125. },
  126. child: Container(
  127. child: Image.asset(
  128. "assets/images/add.png",
  129. width: 160,
  130. height: 160,
  131. ),
  132. ),
  133. ),
  134. buildUploadImage(),
  135. SizedBox(
  136. height: 10,
  137. ),
  138. Container(
  139. width: 150,
  140. child: InkWell(
  141. onTap: () {
  142. // 登录
  143. submitInfo();
  144. },
  145. child: Container(
  146. width: double.infinity,
  147. height: AppUtil.height(80),
  148. padding: EdgeInsets.only(
  149. right: AppUtil.width(20), left: AppUtil.width(20)),
  150. decoration: BoxDecoration(
  151. gradient: const LinearGradient(colors: [
  152. ThemeColor.loignColor,
  153. ThemeColor.loignColor
  154. ]),
  155. borderRadius: BorderRadius.circular(10),
  156. boxShadow: const [
  157. BoxShadow(
  158. offset: Offset(1.0, 5.0),
  159. color: ThemeColor.loignColor,
  160. blurRadius: 5.0,
  161. )
  162. ]),
  163. child: const Center(
  164. child: Text(
  165. "提交",
  166. style: TextStyle(fontSize: 20, color: Colors.white),
  167. ),
  168. ),
  169. ),
  170. ),
  171. ),
  172. const SizedBox(
  173. height: 50,
  174. ),
  175. ],
  176. ),
  177. ),
  178. ),
  179. );
  180. }
  181. Widget buildUploadImage() {
  182. if (_filePath != null) {
  183. return Image.file(
  184. File(_filePath),
  185. width: 160,
  186. height: 160,
  187. );
  188. } else {
  189. return Text("");
  190. }
  191. }
  192. void submitInfo() async {
  193. if (_submitid > 0) {
  194. SharedPreferences prefs = await SharedPreferences.getInstance();
  195. String token = prefs.getString("token");
  196. var uploadTrack = TrackDao.uploadTrack(
  197. token, _userName, _tel, _addresds, _heathStatus, _submitid);
  198. AppUtil.buildToast("提交完成");
  199. Navigator.pop(context);
  200. } else {
  201. AppUtil.buildToast("请先添加核酸检测图片!");
  202. }
  203. }
  204. void chooseImg() async {
  205. FilePickerResult result = await FilePicker.platform.pickFiles(
  206. type: FileType.custom,
  207. allowedExtensions: ['jpg'],
  208. );
  209. if (result != null) {
  210. print('---选择的路径---' + result.files.single.path);
  211. setState(() {
  212. _filePath = result.files.single.path;
  213. });
  214. SharedPreferences prefs = await SharedPreferences.getInstance();
  215. var token = prefs.getString("token");
  216. SubmitEntity submiEntity = await TrackDao.uploadImg(
  217. token, result.files.single.path, result.files.single.name);
  218. AppUtil.buildToast(submiEntity.msg.msg);
  219. _submitid = submiEntity.submitModel.id;
  220. } else {
  221. print('用户停止了选择文件');
  222. }
  223. }
  224. }