submit_page.dart 7.4 KB

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