submit_page.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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: 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().length > 0 ? null : "请输入地址";
  75. },
  76. onChanged: (inputStr) {
  77. _addresds = inputStr;
  78. },
  79. ),
  80. SizedBox(
  81. height: 10,
  82. ),
  83. Align(
  84. child: Text(
  85. "健康状态:",
  86. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  87. ),
  88. alignment: Alignment.centerLeft,
  89. ),
  90. RadioListTile(
  91. title: Text("阴性"),
  92. value: true,
  93. groupValue: _heathStatus,
  94. onChanged: (value) {
  95. setState(() {
  96. _heathStatus = value;
  97. });
  98. },
  99. ),
  100. RadioListTile(
  101. title: Text("阳性"),
  102. value: false,
  103. groupValue: _heathStatus,
  104. onChanged: (value) {
  105. setState(() {
  106. _heathStatus = value;
  107. });
  108. },
  109. ),
  110. SizedBox(
  111. height: 10,
  112. ),
  113. Align(
  114. child: Text(
  115. "核酸检测:",
  116. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  117. ),
  118. alignment: Alignment.centerLeft,
  119. ),
  120. Text("请拍照上传清晰的核酸证明图片"),
  121. InkWell(
  122. onTap: () {
  123. chooseImg();
  124. },
  125. child: Container(
  126. child: Image.asset(
  127. "assets/images/add.png",
  128. width: 160,
  129. height: 160,
  130. ),
  131. ),
  132. ),
  133. buildUploadImage(),
  134. SizedBox(
  135. height: 10,
  136. ),
  137. Container(
  138. width: 150,
  139. child: InkWell(
  140. onTap: () {
  141. // 登录
  142. submitInfo();
  143. },
  144. child: Container(
  145. width: double.infinity,
  146. height: AppUtil.height(80),
  147. padding: EdgeInsets.only(
  148. right: AppUtil.width(20), left: AppUtil.width(20)),
  149. decoration: BoxDecoration(
  150. gradient: const LinearGradient(colors: [
  151. ThemeColor.loignColor,
  152. ThemeColor.loignColor
  153. ]),
  154. borderRadius: BorderRadius.circular(10),
  155. boxShadow: const [
  156. BoxShadow(
  157. offset: Offset(1.0, 5.0),
  158. color: ThemeColor.loignColor,
  159. blurRadius: 5.0,
  160. )
  161. ]),
  162. child: const Center(
  163. child: Text(
  164. "提交",
  165. style: TextStyle(fontSize: 20, color: Colors.white),
  166. ),
  167. ),
  168. ),
  169. ),
  170. ),
  171. const SizedBox(
  172. height: 50,
  173. ),
  174. ],
  175. ),
  176. ),
  177. ),
  178. );
  179. }
  180. Widget buildUploadImage() {
  181. if (_filePath != null) {
  182. return Image.file(
  183. File(_filePath),
  184. width: 160,
  185. height: 160,
  186. );
  187. } else {
  188. return Text("");
  189. }
  190. }
  191. void submitInfo() async {
  192. SharedPreferences prefs = await SharedPreferences.getInstance();
  193. String token = prefs.getString("token") ?? "";
  194. var uploadTrack =
  195. TrackDao.uploadTrack(token, _tel, _addresds, _heathStatus);
  196. }
  197. void chooseImg() async {
  198. FilePickerResult result = await FilePicker.platform.pickFiles(
  199. type: FileType.custom,
  200. allowedExtensions: ['jpg', 'pdf', 'doc'],
  201. );
  202. if (result != null) {
  203. print('---选择的路径---' + result.files.single.path);
  204. setState(() {
  205. _filePath = result.files.single.path;
  206. });
  207. SharedPreferences prefs = await SharedPreferences.getInstance();
  208. var token = prefs.getString("token");
  209. SubmitModel fileEntity = await TrackDao.uploadImg(
  210. token, result.files.single.path, result.files.single.name);
  211. AppUtil.buildToast("fileEntity.msgModel.msg");
  212. prefs.setString("fileName", result.files.single.name);
  213. prefs.setInt("fileid", 11);
  214. } else {
  215. print('用户停止了选择文件');
  216. }
  217. }
  218. }