import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:qiniu_flutter_sdk/qiniu_flutter_sdk.dart'; import 'package:ysairplane2/base/YSBase.dart'; import 'package:ysairplane2/tools/YSNetWorking.dart'; import 'package:ysairplane2/tools/YSTools.dart'; import 'package:image_picker/image_picker.dart'; class YSAddSquare extends StatefulWidget { final bool isPhoto; const YSAddSquare({Key key, this.isPhoto}) : super(key: key); @override _YSAddSquareState createState() => _YSAddSquareState(); } class _YSAddSquareState extends State { ImagePicker _picker = ImagePicker(); TextEditingController _title = TextEditingController(); TextEditingController _content = TextEditingController(); List _images = []; String _tokenStr = ''; String _urlStr = ''; String _videoPath = ''; @override void initState() { Future.delayed(Duration(seconds: 0)).then((value){ _getQiNiuTokenData(); }); super.initState(); } @override Widget build(BuildContext context) { return YSBase( ystitle: '发布', yschild: Column( children: [ Container( padding: EdgeInsets.all(15), height: 120, child: ListView.separated( scrollDirection: Axis.horizontal, itemBuilder: (context,index){ return GestureDetector( onTap: (){ showCupertinoModalPopup( context: context, builder: (context) { return CupertinoActionSheet( actions: [ CupertinoActionSheetAction( child: Text(widget.isPhoto==true?'拍摄照片':'拍摄视频'), onPressed: () { Navigator.pop(context); widget.isPhoto==true?_picker.getImage(source: ImageSource.camera).then((value){ _onUpload(value); }):_picker.getVideo(source: ImageSource.camera).then((value){ _onUpload(value); }); }, ), CupertinoActionSheetAction( child: Text(widget.isPhoto==true?'选择图片':'选择视频'), onPressed: () { Navigator.pop(context); widget.isPhoto==true?_picker.getImage(source: ImageSource.gallery).then((value){ _onUpload(value); }):_picker.getVideo(source: ImageSource.gallery).then((value){ _onUpload(value); }); }, ), ], cancelButton: CupertinoActionSheetAction( child: Text('取消'), onPressed: () { Navigator.pop(context); }, ), ); } ); }, child: (index==_images.length&&widget.isPhoto==true)||(_videoPath.isEmpty&&widget.isPhoto==false)?Container( height: 90, width: 90, padding: EdgeInsets.only(top: 20,bottom: 20), decoration: BoxDecoration( border: Border.all(color: Color(0xFFEDEDED),width: 1), borderRadius: BorderRadius.all(Radius.circular(10)) ), child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Icon(Icons.add,size: 25,color: Color(0xFF9A9A9A),), Text(widget.isPhoto==true?'上传图片':'上传视频',style: TextStyle(fontSize: 14,color: Color(0xFF9A9A9A),decoration: TextDecoration.none),), ], ), ):(_videoPath.isNotEmpty&&widget.isPhoto==false)?Container( height: 90, width: 90, padding: EdgeInsets.only(top: 20,bottom: 20), decoration: BoxDecoration( border: Border.all(color: Color(0xFFEDEDED),width: 1), borderRadius: BorderRadius.all(Radius.circular(10)), color: Colors.black54, ), child: Container( height: 20, width: 20, child: Icon(Icons.play_circle_filled,color: Colors.white,), ), ):Container( height: 90, width: 90, padding: EdgeInsets.only(top: 20,bottom: 20), decoration: BoxDecoration( border: Border.all(color: Color(0xFFEDEDED),width: 1), borderRadius: BorderRadius.all(Radius.circular(10)), image: DecorationImage(image: NetworkImage(_images[index]),fit: BoxFit.fill) ), ), ); }, separatorBuilder: (context,index){ return Container( width: 10, ); }, itemCount: _images.length+1 ), ), GestureDetector( onTap: (){ showModalBottomSheet( context: context, barrierColor: Colors.transparent, backgroundColor: Colors.transparent, isScrollControlled: true, builder: (context){ return SingleChildScrollView( padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), child: Container( height: 50, padding: EdgeInsets.only(left: hsp(30),right: hsp(30)), decoration: BoxDecoration( borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)), color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black26, blurRadius: 10 ) ] ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( width: MediaQuery.of(context).size.width-100, child: CupertinoTextField( placeholder: '说点儿好听的~', style: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal), placeholderStyle: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal), autofocus: true, decoration: BoxDecoration(), controller: _title, maxLength: 20, onSubmitted: (value){ Navigator.pop(context); }, ), ), Icon(Icons.alternate_email,size: 25,color: Color(0xFFA1A1A2),), Icon(Icons.tag_faces,size: 25,color: Color(0xFFA1A1A2),), ], ), ), ); } ); }, child: Container( height: 50, margin: EdgeInsets.only(left: 15,right: 15,top: 5,bottom: 15), decoration: BoxDecoration( border: Border(bottom: BorderSide(color: Color(0xFFE6E6E6),width: 0.5)) ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(_title.text.isEmpty?'填写标题会有很多赞哦~':_title.text,style: TextStyle(fontSize: 16,color: Color(0xFF9A9A9A)),), Text('${_title.text.length}/20',style: TextStyle(fontSize: 17,color: Color(0xFF9A9A9A)),), ], ) ), ), GestureDetector( onTap: (){ showModalBottomSheet( context: context, barrierColor: Colors.transparent, backgroundColor: Colors.transparent, isScrollControlled: true, builder: (context){ return SingleChildScrollView( padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), child: Container( height: 50, padding: EdgeInsets.only(left: hsp(30),right: hsp(30)), decoration: BoxDecoration( borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)), color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black26, blurRadius: 10 ) ] ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Container( width: MediaQuery.of(context).size.width-100, child: CupertinoTextField( placeholder: '说点儿好听的~', style: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal), placeholderStyle: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal), autofocus: true, decoration: BoxDecoration(), controller: _content, onSubmitted: (value){ Navigator.pop(context); }, ), ), Icon(Icons.alternate_email,size: 25,color: Color(0xFFA1A1A2),), Icon(Icons.tag_faces,size: 25,color: Color(0xFFA1A1A2),), ], ), ), ); } ); }, child: Container( height: 200, width: MediaQuery.of(context).size.width-30, margin: EdgeInsets.only(left: 15,right: 15), child: SingleChildScrollView( child: Text(_content.text.isEmpty?'添加正文':_content.text,style: TextStyle(fontSize: 15,color: Color(0xFF9A9A9A)),), ), ), ), Container( width: MediaQuery.of(context).size.width-30, margin: EdgeInsets.only(top: 10,bottom: 10,left: 145), child: Row( children: [ Text('写50字,有机会评为',style: TextStyle(fontSize: 14,color: Color(0xFF9A9A9A)),), Text('优质内容 ',style: TextStyle(fontSize: 14,color: Color(0xFF007AFF)),), Image(image: AssetImage('lib/images/tips.png'),height: 15,width: 15,) ], ), ), Divider(height: 0.5,thickness: 0.5,color: Color(0xFFE6E6E6),indent: 15,endIndent: 15,), GestureDetector( onTap: (){ _postAddSquareData(); }, child: Container( margin: EdgeInsets.only(top: 60), width: MediaQuery.of(context).size.width-70, height: 44, alignment: Alignment.center, decoration: BoxDecoration( color: Color(0xFF007EFF), borderRadius: BorderRadius.all(Radius.circular(22)) ), child: Text('发布',style: TextStyle(fontSize: 18,color: Colors.white),), ), ) ], ), ); } _getQiNiuTokenData() async{ Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/getToken',parameter: {},isLoading: false,isToken: false); if(dict!=null){ _tokenStr = dict['token']; _urlStr = dict['prefix']; } } _onUpload(var value) async { if (value == null) { return; } print(value.path); print(_tokenStr); Storage storage = Storage(); storage.putFile( File(value.path), _tokenStr, options: PutOptions( key: DateTime.now().millisecondsSinceEpoch.toString() +'.' +value.path.split('.').last, ), ).then((result) async{ setState(() { widget.isPhoto==true?_images.add(_urlStr+'${result.key}'):_videoPath = _urlStr+'${result.key}'; }); }); // final syStorage = new SyFlutterQiniuStorage(); // //监听上传进度 // syStorage.onChanged().listen((dynamic percent) { // print(percent); // }); // //上传文件 // var result = await syStorage.upload(value.path, _tokenStr, DateTime.now().millisecondsSinceEpoch.toString() +'.' +value.path.split('.').last); // if(result.success==true){ // setState(() { // widget.isPhoto==true?_images.add(_urlStr+'${result.result['key']}'):_videoPath = _urlStr+'${result.result['key']}'; // }); // } // print(result); } // //取消上传 // _onCancel() { // SyFlutterQiniuStorage.cancelUpload(); // } //0为视频,1为文章 _postAddSquareData() async{ if(widget.isPhoto==true&&_images.length==0){ ysFlutterToast(context, '请添加图片'); return; } if(widget.isPhoto==false&&_videoPath.isEmpty){ ysFlutterToast(context, '请添加视频'); return; } if(_title.text.isEmpty){ ysFlutterToast(context, '请输入标题'); return; } if(_content.text.isEmpty){ ysFlutterToast(context, '请输入内容'); return; } Map request ={}; request['typeId'] = 1; if(widget.isPhoto==true){ request['type'] = 1; request['topicTitle'] = _title.text; request['topicContent'] = _content.text; request['topicPic'] = _images.join(','); }else{ request['type'] = 0; request['videoTitle'] = _title.text; request['videoContent'] = _content.text; request['videoPath'] = _videoPath; } Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/piazza/SquareCommunity/publish',parameter: request,isLoading: true,isToken: true,refresh: (){ _postAddSquareData(); }); if(dict!=null){ Navigator.of(context).pop(''); } } }