import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:image_picker/image_picker.dart'; import 'package:liftmanager/net/api_service.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/utils/theme_utils.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:liftmanager/widgets/click_item.dart'; import 'package:liftmanager/widgets/my_button.dart'; import 'package:liftmanager/widgets/selected_image.dart'; import 'package:oktoast/oktoast.dart'; class YearlyUploadPage extends StatefulWidget { YearlyUploadPage(this.type, this.id); int type; //0自检报告 1检验报告 2上传年检报告和合格证 String id; @override State createState() { return YearlyUploadPageState(); } } class YearlyUploadPageState extends State { String _stopTime = ""; File _imageFile; File _imageFilezj2; File _imageFilezj3; File _imageFile2; List listStr = ["自检", "检验", "年检"]; /// 选择时间 Future _selectTime() async { DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {}, onConfirm: (date) { _stopTime = "${date.toString().split(" ")[0]}"; setState(() {}); }, currentTime: DateTime.now(), locale: LocaleType.zh); } ///保存自检报告 _saveSelfCheck() { if (_stopTime.length == 0) { toasts("请选择${listStr[widget.type]}时间"); return; } if (_imageFile == null) { toasts("请上传${listStr[widget.type]}图片"); return; } List list = [_imageFile]; if(_imageFilezj2 != null){ list.add(_imageFilezj2); } if(_imageFilezj3 != null){ list.add(_imageFilezj3); } showLoading(context, "正在提交..."); ApiService(context: context).uploadMore( list, onSuccess: (List imgUrls) { ApiService(context: context).yearlySelfCheck(widget.id, _stopTime, imgUrls.toString().substring(1,imgUrls.toString().length-1), onSuccess: (data) { dismissLoading(context); showAlert(context, "提示", "提交成功", "确定", () { NavigatorUtils.goBack(context); NavigatorUtils.goBackWithParams( context, {"time": _stopTime, "img": imgUrls.toString().substring(1,imgUrls.toString().length-1)}); }); }, onError: (code, msg) { toasts(msg); dismissLoading(context); }); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } ///检验结果 _saveCheck() { if (_stopTime.length == 0) { toasts("请选择${listStr[widget.type]}时间"); return; } if (_imageFile == null) { toasts("请上传${listStr[widget.type]}图片"); return; } showLoading(context, "正在提交..."); ApiService(context: context).upload( _imageFile.path, onSuccess: (String imgUrl) { ApiService(context: context).yearlyCheck( widget.id, _stopTime, imgUrl, onSuccess: (data) { dismissLoading(context); showAlert(context, "提示", "提交成功", "确定", () { NavigatorUtils.goBack(context); NavigatorUtils.goBackWithParams( context, {"time": _stopTime, "img": imgUrl}); }); }, onError: (code, msg) { toasts(msg); dismissLoading(context); }); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } ///合格证 _saveFinish() { if (_imageFile == null) { toasts("请上传${listStr[widget.type]}图片"); return; } if (_imageFile2 == null) { toasts("请上传合格证图片"); return; } showLoading(context, "正在提交..."); ApiService(context: context).uploadMore([_imageFile, _imageFile2], onSuccess: (List imgList) { if (imgList.length != 2) { dismissLoading(context); toasts("上传图片失败"); return; } ApiService(context: context).yearlyFinish( widget.id, imgList[0], imgList[1], onSuccess: (data) { dismissLoading(context); showAlert(context, "提示", "提交成功", "确定", () { NavigatorUtils.goBack(context); NavigatorUtils.goBackWithParams( context, {"img1": imgList[0], "img2": imgList[1]}); }); }, onError: (code, msg) { showToast(msg); dismissLoading(context); }); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } ///选择图片 void selectPicker(int index) { showDialog( context: context, builder: (BuildContext context) { return SimpleDialog( title: Text("选择方式"), children: ["拍照", '从手机相册选择'].map((String value) { print("$value"); return SimpleDialogOption( child: Text( "${value}", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500), ), onPressed: () { _getImage(value == '拍照' ? 1 : 0, index); Navigator.of(context).pop(); }, ); }).toList()); }); } void _getImage(int key, int index) async { try { var file = await ImagePicker.pickImage( source: key == 1 ? ImageSource.camera : ImageSource.gallery, maxWidth: 800, imageQuality: 95); if (index == 1) { _imageFile = file; } else if (index == 22) { _imageFilezj2 = file; } else if (index == 33) { _imageFilezj3 = file; } else { _imageFile2 = file; } setState(() {}); } catch (e) { toasts("没有权限,无法打开相册!"); } } @override Widget build(BuildContext context) { print(widget.type); print(_imageFile); return Scaffold( backgroundColor: ThemeUtils.getTabsBg(context), appBar: MyAppBar( centerTitle: "上传${listStr[widget.type]}报告", ), body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Offstage( offstage: widget.type == 2, child: ClickItem( title: "${listStr[widget.type]}时间", hintText: "请选择${listStr[widget.type]}时间", content: "${_stopTime}", onTap: () { _selectTime(); }, ), ), Container( padding: EdgeInsets.fromLTRB(15, 10, 15, 10), child: Text("${listStr[widget.type]}报告"), ), Row( children: [ Container( padding: EdgeInsets.all(15), child: SelectedImage( image: _imageFile, onTap: () { selectPicker(1); }) ), Offstage( offstage: (widget.type != 0 || _imageFile ==null), child: Container( padding: EdgeInsets.all(15), child: SelectedImage( image: _imageFilezj2, onTap: () { selectPicker(22); }) ), ), Offstage( offstage: widget.type != 0 || _imageFilezj2 ==null, child: Container( padding: EdgeInsets.all(15), child: SelectedImage( image: _imageFilezj3, onTap: () { selectPicker(33); }) ), ), ], ), Offstage( offstage: widget.type != 2, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( padding: EdgeInsets.fromLTRB(15, 10, 15, 10), child: Text("合格证"), ), Container( padding: EdgeInsets.all(15), color: Colors.white, child: SelectedImage( image: _imageFile2, onTap: () { selectPicker(2); }), ), ], ), ), Container( child: Padding( padding: const EdgeInsets.all(16), child: MyButton( fontSize: 14, onPressed: () { print("${widget.type}"); if (widget.type == 0) { _saveSelfCheck(); } else if (widget.type == 1) { _saveCheck(); } else { _saveFinish(); } }, text: "确定", ), )) ], ), ); } }