import 'package:flutter/material.dart'; import 'package:liftmanager/res/gaps.dart'; import 'package:liftmanager/net/api_service.dart'; import 'package:liftmanager/utils/toast.dart'; import 'package:liftmanager/widgets/app_bar.dart'; import 'package:liftmanager/internal/search/search_router.dart'; import 'package:liftmanager/widgets/app_search_bar.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/routers/fluro_navigator.dart'; import 'package:liftmanager/widgets/load_image.dart'; import 'package:liftmanager/widgets/selected_image_change.dart'; import 'package:image_picker/image_picker.dart'; import 'dart:io'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:liftmanager/internal/bbs/bbs_router.dart'; import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:liftmanager/common/common.dart'; import 'package:flustars/flustars.dart' as flustars; import 'package:liftmanager/utils/theme_utils.dart'; import 'package:chewie/chewie.dart'; import 'package:liftmanager/utils/oss_upload.dart'; import 'package:video_player/video_player.dart'; import 'package:liftmanager/widgets/selected_video_change.dart'; import 'package:liftmanager/utils/utils.dart'; import 'package:orientation/orientation.dart'; import 'package:flutter/services.dart'; import 'package:liftmanager/utils/fast_notification.dart'; class InterrogationPage extends StatefulWidget { InterrogationPage(this.brandName,this.brandId); final String brandName; final String brandId; @override State createState() { return InterrogationPageState(); } } class InterrogationPageState extends State { // NewsDetailItem item = NewsDetailItem(); List imagesUrl = []; String videoUrl; String str; double percent = 0.0; String addressName = ""; VideoPlayerController _controller; VideoPlayerController _controllerFile; @override void initState() { super.initState(); print(widget.brandName); print(widget.brandId); print(65656); } //图片 upLoadFileOnce(path) { showLoading(context, "正在上传..."); NewApiService().upload(path, onSuccess: (res) { // imagesUrl.add(res.path); dismissLoading(context); setState(() { imagesUrl.add(res.pathUrl); }); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } //视频 upLoadFileOnceVideo(path) { showLoading(context, "正在上传..."); NewApiService().upload(path, onSuccess: (res) { // imagesUrl.add(res.path); dismissLoading(context); setState(() { // videoUrl.add(res.pathUrl); // imagesUrlVideo.add(res.coverUrl); }); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } ///选择图片 void selectPicker(type) { 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: () { if(type=="image"){ _getImage(value == '拍照' ? 1 : 0); }else if (type=="video") { _getVideo(value == '拍照' ? 1 : 0); } Navigator.of(context).pop(); }, ); }).toList()); }); } void _getImage(int key) async { try { var _imageFile = await ImagePicker.pickImage( source: key == 1 ? ImageSource.camera : ImageSource.gallery, maxWidth: 800, imageQuality: 95); print(_imageFile); print(3333); if (_imageFile != null) { // images.add(_imageFile); upLoadFileOnce(_imageFile.path); // setState(() {}); } } catch (e) { toasts("没有权限,无法打开相册!"); } } void _getVideo(int key) async { print(key); print(333); try { await ImagePicker.pickVideo( source: key == 1 ? ImageSource.camera : ImageSource.gallery, ) .then((File f) async{ if (f != null) { _controllerFile = VideoPlayerController.file(f); _controllerFile.initialize().then((val){ _controllerFile.setLooping(true); int seconds = _controllerFile.value.duration.inSeconds; print("视频时长:$seconds"); int fileSize=f.lengthSync();//单位B print("视频大小:${fileSize}"); print("视频大小:${f.path}"); if (seconds <= 300) { _uploadImage(f.path); // upLoadFileOnce(_imageFile.path); // setState(() {}); }else { toasts("视频时长不能大于5分钟!"); } }).catchError((error){ print(error); print("error"); toasts("上传失败,不支持此格式"); });} }) ; } catch (e) { toasts("没有权限,无法打开相册!"); } } void _uploadImage(filePath) async { showPercent(context, (){ dismissLoading(context); toasts("上传失败"); },(){ if(videoUrl == null && str != null){ setState(() { videoUrl = str; print("videoUrl:"+videoUrl); dismissLoading(context); toasts("上传成功"); }); } }); String uploadName = OssUtil.instance.getImageUploadName(filePath); await NewApiService.uploadImage(context, uploadName, filePath).then((data) { if (data.statusCode == 200) { str = NewApiUrl.URL_UPLOAD_IMAGE_OSS + "/" + uploadName; print("str:"+str); print(videoUrl); if(str != null){ Map obj = { "uploadName":uploadName, "success":true }; FastNotification.push("percent",obj); } }else { Map obj = { "uploadName":uploadName, "success":false }; FastNotification.push("percent",obj); } }).catchError((data) { Map obj = { "uploadName":uploadName, "success":false }; FastNotification.push("percent",obj); }); } // 焦点控制 // FocusNode _focusNode1 = new FocusNode(); GlobalKey _formKey= new GlobalKey(); TextEditingController _questionController = new TextEditingController(); TextEditingController _methodController = new TextEditingController(); FocusNode blankNode = FocusNode(); @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; // 监听FocusNode // _focusNode1.addListener((){ // // _focusNode1.hasFocus 是否聚焦 // print(_focusNode1.hasFocus); // }); return Scaffold( resizeToAvoidBottomPadding: false,//不让键盘弹上去 appBar: MyAppBar( centerTitle: "我要提问", ), body: GestureDetector( onTap: (){ // 点击空白页面关闭键盘 FocusScope.of(context).requestFocus(blankNode); }, child: Stack( children:[ Container( padding: EdgeInsets.only(bottom:ScreenUtil().setWidth(80)), child: ListView( children: [ Form( key: _formKey, //设置globalKey,用于后面获取FormState // autovalidate: true, //开启自动校验 child: Column( children: [ Container( padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(10)), decoration: BoxDecoration( border: Border( bottom: BorderSide(width: 0.5, color: Colours.line), ), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children:[ Text( "电梯品牌", style: TextStyle( // fontSize:ScreenUtil().setSp(14) ), textAlign:TextAlign.start, ), Text( widget.brandName, style: TextStyle( color:Color(0xff999999), // fontSize:ScreenUtil().setSp(14) ), textAlign:TextAlign.start, ), ] ), ), Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [ Container( padding:EdgeInsets.only(left:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(5)), child: Text( "问题描述", style: TextStyle( // fontSize:ScreenUtil().setSp(14), ), textAlign:TextAlign.left, ), ), ], ), Container( height:120, padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(20)), child: TextFormField( // autofocus: true, maxLength: 50, cursorColor: Color(0xffcccccc), controller: _questionController, maxLines:5, decoration: InputDecoration( contentPadding: EdgeInsets.all(0), hintText: '请详细描述事故问题和现场情况', hintStyle:TextStyle( color: Color(0xffcccccc), // fontSize: ScreenUtil().setSp(14), ), focusedBorder: InputBorder.none, border: InputBorder.none, // filled: true, // 背景色 // fillColor: Colors.cyan.withAlpha(35), // icon: Icon(Icons.person) ), // 校验 validator: (val) { return val.trim().length > 0 ? null : "不能为空"; } ), ), SizedBox( height:6, child: Container( color:ThemeUtils.getDialogTextFieldColor(context) ), ), Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [ Container( padding:EdgeInsets.only(left:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(5)), child: Text( "已尝试方法", style: TextStyle( // fontSize:ScreenUtil().setSp(14), ), textAlign:TextAlign.left, ), ), ], ), Container( height:120, padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(20)), child: TextFormField( // autofocus: true, maxLength: 50, cursorColor: Color(0xffcccccc), controller: _methodController, maxLines:5, decoration: InputDecoration( contentPadding: EdgeInsets.all(0), hintText: '请详细描述已尝试方法以及结果', hintStyle:TextStyle( color: Color(0xffcccccc), // fontSize: ScreenUtil().setSp(14), ), focusedBorder: InputBorder.none, border: InputBorder.none, // filled: true, // 背景色 // fillColor: Colors.cyan.withAlpha(35), // icon: Icon(Icons.person) ), // 校验 validator: (val) { return val.trim().length > 0 ? null : "不能为空"; } ), ), SizedBox( height:6, child: Container( color:ThemeUtils.getDialogTextFieldColor(context) ), ), Container( width: width, padding: EdgeInsets.only(left:15,top:15), child: Text( "上传图片:", style: TextStyle( fontSize: ScreenUtil() .setSp(14), ), textAlign: TextAlign.left, ), ), Container( color: ThemeUtils.getTabsBg(context), child: GridView.builder( shrinkWrap: true, padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0), physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, childAspectRatio: 1.18), itemCount: imagesUrl.length >= 9 ? 9 : imagesUrl.length + 1, itemBuilder: (_, index) { return Stack( children: [ Center( child: SelectedImage( image: index < imagesUrl.length ? imagesUrl[index] : null, index:index, onTap: () { if(index >= imagesUrl.length){ selectPicker("image"); } FocusScope.of(context).requestFocus(FocusNode()); print(index); print(imagesUrl); }), ), index < imagesUrl.length?Positioned( top:0, right:0, child:GestureDetector( onTap: (){ print(index); imagesUrl.remove(imagesUrl[index]); setState(() { }); }, child:Icon( IconData( 0xe62a, fontFamily:"myfont" ), size: 24.0, color:Color(0xff999999), ), ) ):Container(child: null,) ], ); }, ) ), SizedBox( height:6, child: Container( color:ThemeUtils.getDialogTextFieldColor(context) ), ), Container( width: width, padding: EdgeInsets.only(left:15,top:15), child: Text( "上传视频:", style: TextStyle( fontSize: ScreenUtil() .setSp(14), ), textAlign: TextAlign.left, ), ), Container( width: width, padding: EdgeInsets.only(left:15,bottom:15), child: Text( "(建议时长3分钟,建议大小50M)", style: TextStyle( color: Colors.red, fontSize: ScreenUtil() .setSp(14), ), textAlign: TextAlign.left, ), ), // Container( // color: ThemeUtils.getTabsBg(context), // child: GridView.builder( // shrinkWrap: true, // padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0), // physics: NeverScrollableScrollPhysics(), // gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( // crossAxisCount: 3, childAspectRatio: 1.18), // itemCount: videoUrl.length >= 1 ? 1 : videoUrl.length + 1, // itemBuilder: (_, index) { // return Stack( // children: [ // Center( // child: SelectedImage( // image: index < videoUrl.length ? imagesUrlVideo[index] : null, // index:index, // onTap: () { // if(index >= videoUrl.length){ // selectPicker("video"); // } // FocusScope.of(context).requestFocus(FocusNode()); // print(index); // print(videoUrl); // }), // ), // index < videoUrl.length?Positioned( // top:0, // right:0, // child:GestureDetector( // onTap: (){ // print(index); // videoUrl.remove(videoUrl[index]); // imagesUrlVideo.remove(imagesUrlVideo[index]); // setState(() { // }); // }, // child:Icon( // IconData( // 0xe62a, // fontFamily:"myfont" // ), // size: 24.0, // color:Color(0xff999999), // ), // ) // ):Container(child: null,) // ], // ); // }, // ) // ), Container( color: ThemeUtils.getDialogTextFieldColor(context), child: GridView.builder( shrinkWrap: true, padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0), physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 1, childAspectRatio: 1.18), itemCount: 1, itemBuilder: (_, index) { return Stack( children: [ Center( child: SelectedVideo( image: videoUrl, index: index, videoPlay:videoPlay(), onTap: () { if(videoUrl == null){ selectPicker("video"); } }, ), ), videoUrl != null ? Positioned( top: 0, right: 0, child: GestureDetector( onTap: () { print(index); // imagesUrl = null; setState(() { videoUrl = null; str = null; _controller.pause(); // player.reset(); }); }, child: Icon( IconData(0xe62a, fontFamily: "myfont"), size: 24.0, color: Color(0xff999999), ), ), ) : Container( child: null, ) ], ); }, ), ), ], ), ) ] ) ), Positioned( bottom:0, left:0, child:Container( width: width, padding: EdgeInsets.only(top:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(15),left:ScreenUtil().setWidth(25),right:ScreenUtil().setWidth(25)), color:ThemeUtils.getDialogTextFieldColor(context), child: Container( height:ScreenUtil().setWidth(44), decoration: BoxDecoration( borderRadius: BorderRadius.circular(22.0), gradient: const LinearGradient( colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]), ), child: FlatButton( // padding: EdgeInsets.all(15.0), child: Text("提交"), textColor: Colors.white, // textColor: Colors.white, onPressed: () { // if(imagesUrl.length<1){ // toasts("请上传图片"); // return; // } String imgs = ""; if(imagesUrl.length > 0){ imgs = imagesUrl.join(","); } // String videoImgs = ""; // if(videoUrl.length > 0){ // videoImgs = videoUrl.join(","); // } if((_formKey.currentState as FormState).validate()){ dynamic obj = { "dataTable":"1", "brandId":widget.brandId, "expression":_questionController.text, "usedMethods":_methodController.text, "imgs":imgs, "videoUrl":videoUrl, "createUserId":int.parse(flustars.SpUtil.getString(Constant.userId)), "chargerId":1 }; showLoading(context, "正在提交..."); NewApiService().createCase(obj, onSuccess: (res) { dismissLoading(context); toasts("提交成功"); FocusScope.of(context).requestFocus(FocusNode()); print(res); print(999999); setDataId(res); setType("1"); print(widget.brandId); if(_controller!=null){ _controller.pause(); } NavigatorUtils.push(context, "${BbsRouter.expertList}?id=${widget.brandId}"); }, onError: (code, msg) { dismissLoading(context); toasts(msg); }); } }, ), ), ) ) ] ), ), ); } void setDataId (int id)async{ SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setInt("dataId", id); } void setType (String type)async{ SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString("questionType", type); } Widget videoPlay() { _controller = VideoPlayerController.network( Utils.getImagePath(videoUrl) // imgFontUrl + detailObj.url ); double width = MediaQuery.of(context).size.width; return // Container( // width: width, // height: width*0.6, // alignment: Alignment.center, // child: FijkView( // player: player, // color: Colors.black, // fit:FijkFit.fill, // // cover: NetworkImage(detailObj.cover), // // cover: new Image(image: detailObj.cover!=null&& detailObj.cover!=""?NetworkImage(detailObj.cover):AssetImage("assets/images/video_image.png"),).image, // // // ), // ); Container( padding: EdgeInsets.only( left: ScreenUtil().setWidth(15), right: ScreenUtil().setWidth(15), top: ScreenUtil().setWidth(15)), child: ClipRRect( borderRadius: BorderRadius.circular(5), child: new Chewie( controller: ChewieController( videoPlayerController: // VideoPlayerController.network( // imgFontUrl + detailObj.url // ), _controller, aspectRatio: 3 / 2, allowFullScreen:false, autoPlay: false, looping: true, // startAt: Duration(seconds: 1,minutes: 1), showControls: true, deviceOrientationsAfterFullScreen:[DeviceOrientation.portraitUp], // 占位图 // placeholder: Image.network( // imgFontUrl+detailObj.cover, // fit: BoxFit.contain, // ), // 是否在 UI 构建的时候就加载视频 autoInitialize: true, // 拖动条样式颜色 materialProgressColors: new ChewieProgressColors( playedColor: Colors.red, handleColor: Colors.blue, backgroundColor: Colors.grey, bufferedColor: Colors.lightGreen, ), ), ), ) ); } }