123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- 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/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<StatefulWidget> createState() {
- return YearlyUploadPageState();
- }
- }
- class YearlyUploadPageState extends State<YearlyUploadPage> {
- String _stopTime = "";
- File _imageFile;
- File _imageFilezj2;
- File _imageFilezj3;
- File _imageFile2;
- List<String> listStr = ["自检", "检验", "年检"];
- /// 选择时间
- Future<void> _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<File> list = [_imageFile];
- if(_imageFilezj2 != null){
- list.add(_imageFilezj2);
- }
- if(_imageFilezj3 != null){
- list.add(_imageFilezj3);
- }
- showLoading(context, "正在提交...");
- ApiService(context: context).uploadMore(
- list, onSuccess: (List<String> 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<String> 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: <Widget>[
- 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: <Widget>[
- 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: <Widget>[
- 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: "确定",
- ),
- ))
- ],
- ),
- );
- }
- }
|