import 'dart:io'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:flutter_tracker/dio/track_dao.dart'; import 'package:flutter_tracker/model/config.dart'; import 'package:flutter_tracker/model/submit_model.dart'; import 'package:flutter_tracker/utils/app_util.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Description: /// Time : 2021年12月03日 Friday /// Author : liuyuqi.gov@msncn class SubmitPage extends StatefulWidget { const SubmitPage({Key key}) : super(key: key); @override _SubmitPageState createState() => _SubmitPageState(); } class _SubmitPageState extends State { final TextEditingController _controllerUsn = TextEditingController(); final TextEditingController _controllerTel = TextEditingController(); final TextEditingController _controllerAddress = TextEditingController(); final GlobalKey _formKey = GlobalKey(); String _userName = ""; String _tel = ""; String _addresds = ""; int _heathStatus = 1; int _submitid = 0; String _filePath; Size get _size => MediaQuery.of(context).size; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("疫情上报"), ), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.only(left: 8.0, right: 8), child: Column( children: [ TextFormField( autofocus: true, controller: _controllerUsn, decoration: const InputDecoration( labelText: "姓名:", hintText: "请输入真实姓名", icon: Icon(Icons.person)), // 校验用户名 validator: (v) { return v.trim().length > 0 ? null : "姓名不能为空"; }, onChanged: (inputStr) { _userName = inputStr; }, ), TextFormField( controller: _controllerTel, decoration: const InputDecoration( labelText: "电话:", hintText: "联系电话", icon: Icon(Icons.phone)), validator: (v) { return v.trim().length > 11 ? null : "手机号错误"; }, onChanged: (inputStr) { _tel = inputStr; }, ), TextFormField( controller: _controllerAddress, decoration: const InputDecoration( labelText: "住址:", hintText: "请输入家庭住址", icon: Icon(Icons.house)), validator: (v) { return v.trim().length > 0 ? null : "请输入地址"; }, onChanged: (inputStr) { _addresds = inputStr; }, ), SizedBox( height: 10, ), Align( child: Text( "健康状态:", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), alignment: Alignment.centerLeft, ), RadioListTile( title: Text("阴性"), value: 1, groupValue: _heathStatus, onChanged: (value) { setState(() { _heathStatus = value; }); }, ), RadioListTile( title: Text("阳性"), value: 2, groupValue: _heathStatus, onChanged: (value) { setState(() { _heathStatus = value; }); }, ), SizedBox( height: 10, ), Align( child: Text( "核酸检测:", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), alignment: Alignment.centerLeft, ), Text("请拍照上传清晰的核酸证明图片"), InkWell( onTap: () { chooseImg(); }, child: Container( child: Image.asset( "assets/images/add.png", width: 160, height: 160, ), ), ), buildUploadImage(), SizedBox( height: 10, ), Container( width: 150, child: InkWell( onTap: () { // 登录 submitInfo(); }, child: Container( width: double.infinity, height: AppUtil.height(80), padding: EdgeInsets.only( right: AppUtil.width(20), left: AppUtil.width(20)), decoration: BoxDecoration( gradient: const LinearGradient(colors: [ ThemeColor.loignColor, ThemeColor.loignColor ]), borderRadius: BorderRadius.circular(10), boxShadow: const [ BoxShadow( offset: Offset(1.0, 5.0), color: ThemeColor.loignColor, blurRadius: 5.0, ) ]), child: const Center( child: Text( "提交", style: TextStyle(fontSize: 20, color: Colors.white), ), ), ), ), ), const SizedBox( height: 50, ), ], ), ), ), ); } Widget buildUploadImage() { if (_filePath != null) { return Image.file( File(_filePath), width: 160, height: 160, ); } else { return Text(""); } } void submitInfo() async { if (_submitid > 0) { SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString("token"); var uploadTrack = TrackDao.uploadTrack( token, _userName, _tel, _addresds, _heathStatus, _submitid); AppUtil.buildToast("提交完成"); Navigator.pop(context); } else { AppUtil.buildToast("请先添加核酸检测图片!"); } } void chooseImg() async { FilePickerResult result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['jpg'], ); if (result != null) { print('---选择的路径---' + result.files.single.path); setState(() { _filePath = result.files.single.path; }); SharedPreferences prefs = await SharedPreferences.getInstance(); var token = prefs.getString("token"); SubmitEntity submiEntity = await TrackDao.uploadImg( token, result.files.single.path, result.files.single.name); AppUtil.buildToast(submiEntity.msg.msg); _submitid = submiEntity.submitModel.id; } else { print('用户停止了选择文件'); } } }