YSAddSquare.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:ysairplane/base/YSBase.dart';
  4. import 'package:ysairplane/tools/YSNetWorking.dart';
  5. import 'package:ysairplane/tools/YSTools.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:sy_flutter_qiniu_storage/sy_flutter_qiniu_storage.dart';
  8. class YSAddSquare extends StatefulWidget {
  9. final bool isPhoto;
  10. const YSAddSquare({Key key, this.isPhoto}) : super(key: key);
  11. @override
  12. _YSAddSquareState createState() => _YSAddSquareState();
  13. }
  14. class _YSAddSquareState extends State<YSAddSquare> {
  15. ImagePicker _picker = ImagePicker();
  16. TextEditingController _title = TextEditingController();
  17. TextEditingController _content = TextEditingController();
  18. List _images = [];
  19. String _tokenStr = '';
  20. String _urlStr = '';
  21. String _videoPath = '';
  22. @override
  23. void initState() {
  24. Future.delayed(Duration(seconds: 0)).then((value){
  25. _getQiNiuTokenData();
  26. });
  27. super.initState();
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return YSBase(
  32. ystitle: '发布',
  33. yschild: Column(
  34. children: [
  35. Container(
  36. padding: EdgeInsets.all(15),
  37. height: 120,
  38. child: ListView.separated(
  39. scrollDirection: Axis.horizontal,
  40. itemBuilder: (context,index){
  41. return GestureDetector(
  42. onTap: (){
  43. showCupertinoModalPopup(
  44. context: context,
  45. builder: (context) {
  46. return CupertinoActionSheet(
  47. actions: <Widget>[
  48. CupertinoActionSheetAction(
  49. child: Text(widget.isPhoto==true?'拍摄照片':'拍摄视频'),
  50. onPressed: () {
  51. Navigator.pop(context);
  52. widget.isPhoto==true?_picker.getImage(source: ImageSource.camera).then((value){
  53. _onUpload(value);
  54. }):_picker.getVideo(source: ImageSource.camera).then((value){
  55. _onUpload(value);
  56. });
  57. },
  58. ),
  59. CupertinoActionSheetAction(
  60. child: Text(widget.isPhoto==true?'选择图片':'选择视频'),
  61. onPressed: () {
  62. Navigator.pop(context);
  63. widget.isPhoto==true?_picker.getImage(source: ImageSource.gallery).then((value){
  64. _onUpload(value);
  65. }):_picker.getVideo(source: ImageSource.gallery).then((value){
  66. _onUpload(value);
  67. });
  68. },
  69. ),
  70. ],
  71. cancelButton: CupertinoActionSheetAction(
  72. child: Text('取消'),
  73. onPressed: () {
  74. Navigator.pop(context);
  75. },
  76. ),
  77. );
  78. }
  79. );
  80. },
  81. child: (index==_images.length&&widget.isPhoto==true)||(_videoPath.isEmpty&&widget.isPhoto==false)?Container(
  82. height: 90,
  83. width: 90,
  84. padding: EdgeInsets.only(top: 20,bottom: 20),
  85. decoration: BoxDecoration(
  86. border: Border.all(color: Color(0xFFEDEDED),width: 1),
  87. borderRadius: BorderRadius.all(Radius.circular(10))
  88. ),
  89. child: Column(
  90. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  91. children: [
  92. Icon(Icons.add,size: 25,color: Color(0xFF9A9A9A),),
  93. Text(widget.isPhoto==true?'上传图片':'上传视频',style: TextStyle(fontSize: 14,color: Color(0xFF9A9A9A),decoration: TextDecoration.none),),
  94. ],
  95. ),
  96. ):(_videoPath.isNotEmpty&&widget.isPhoto==false)?Container(
  97. height: 90,
  98. width: 90,
  99. padding: EdgeInsets.only(top: 20,bottom: 20),
  100. decoration: BoxDecoration(
  101. border: Border.all(color: Color(0xFFEDEDED),width: 1),
  102. borderRadius: BorderRadius.all(Radius.circular(10)),
  103. color: Colors.black54,
  104. ),
  105. child: Container(
  106. height: 20,
  107. width: 20,
  108. child: Icon(Icons.play_circle_filled,color: Colors.white,),
  109. ),
  110. ):Container(
  111. height: 90,
  112. width: 90,
  113. padding: EdgeInsets.only(top: 20,bottom: 20),
  114. decoration: BoxDecoration(
  115. border: Border.all(color: Color(0xFFEDEDED),width: 1),
  116. borderRadius: BorderRadius.all(Radius.circular(10)),
  117. image: DecorationImage(image: NetworkImage(_images[index]),fit: BoxFit.fill)
  118. ),
  119. ),
  120. );
  121. },
  122. separatorBuilder: (context,index){
  123. return Container(
  124. width: 10,
  125. );
  126. },
  127. itemCount: _images.length+1
  128. ),
  129. ),
  130. GestureDetector(
  131. onTap: (){
  132. Navigator.push(context, PopRoute(child: BottomInputDialog(
  133. height: 50,
  134. inputView: Row(
  135. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  136. children: [
  137. Container(
  138. width: MediaQuery.of(context).size.width-100,
  139. child: CupertinoTextField(
  140. placeholder: '说点儿好听的~',
  141. style: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  142. placeholderStyle: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  143. autofocus: true,
  144. decoration: BoxDecoration(),
  145. controller: _title,
  146. maxLength: 20,
  147. ),
  148. ),
  149. Icon(Icons.alternate_email,size: 25,color: Color(0xFFA1A1A2),),
  150. Icon(Icons.tag_faces,size: 25,color: Color(0xFFA1A1A2),),
  151. ],
  152. ),
  153. )));
  154. },
  155. child: Container(
  156. height: 50,
  157. margin: EdgeInsets.only(left: 15,right: 15,top: 5,bottom: 15),
  158. decoration: BoxDecoration(
  159. border: Border(bottom: BorderSide(color: Color(0xFFE6E6E6),width: 0.5))
  160. ),
  161. child: Row(
  162. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  163. children: [
  164. Text(_title.text.isEmpty?'填写标题会有很多赞哦~':_title.text,style: TextStyle(fontSize: 16,color: Color(0xFF9A9A9A)),),
  165. Text('${_title.text.length}/20',style: TextStyle(fontSize: 17,color: Color(0xFF9A9A9A)),),
  166. ],
  167. )
  168. ),
  169. ),
  170. GestureDetector(
  171. onTap: (){
  172. Navigator.push(context, PopRoute(child: BottomInputDialog(
  173. height: 50,
  174. inputView: Row(
  175. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  176. children: [
  177. Container(
  178. width: MediaQuery.of(context).size.width-100,
  179. child: CupertinoTextField(
  180. placeholder: '说点儿好听的~',
  181. style: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  182. placeholderStyle: TextStyle(fontSize: 15,color: Color(0xFF5A5A5A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  183. autofocus: true,
  184. decoration: BoxDecoration(),
  185. controller: _content,
  186. ),
  187. ),
  188. Icon(Icons.alternate_email,size: 25,color: Color(0xFFA1A1A2),),
  189. Icon(Icons.tag_faces,size: 25,color: Color(0xFFA1A1A2),),
  190. ],
  191. ),
  192. )));
  193. },
  194. child: Container(
  195. height: 200,
  196. width: MediaQuery.of(context).size.width-30,
  197. margin: EdgeInsets.only(left: 15,right: 15),
  198. child: SingleChildScrollView(
  199. child: Text(_content.text.isEmpty?'添加正文':_content.text,style: TextStyle(fontSize: 15,color: Color(0xFF9A9A9A)),),
  200. ),
  201. ),
  202. ),
  203. Container(
  204. width: MediaQuery.of(context).size.width-30,
  205. margin: EdgeInsets.only(top: 10,bottom: 10,left: 145),
  206. child: Row(
  207. children: [
  208. Text('写50字,有机会评为',style: TextStyle(fontSize: 14,color: Color(0xFF9A9A9A)),),
  209. Text('优质内容 ',style: TextStyle(fontSize: 14,color: Color(0xFF007AFF)),),
  210. Image(image: AssetImage('lib/images/tips.png'),height: 15,width: 15,)
  211. ],
  212. ),
  213. ),
  214. Divider(height: 0.5,thickness: 0.5,color: Color(0xFFE6E6E6),indent: 15,endIndent: 15,),
  215. GestureDetector(
  216. onTap: (){
  217. _postAddSquareData();
  218. },
  219. child: Container(
  220. margin: EdgeInsets.only(top: 60),
  221. width: MediaQuery.of(context).size.width-70,
  222. height: 44,
  223. alignment: Alignment.center,
  224. decoration: BoxDecoration(
  225. color: Color(0xFF007EFF),
  226. borderRadius: BorderRadius.all(Radius.circular(22))
  227. ),
  228. child: Text('发布',style: TextStyle(fontSize: 18,color: Colors.white),),
  229. ),
  230. )
  231. ],
  232. ),
  233. );
  234. }
  235. _getQiNiuTokenData() async{
  236. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/getToken',parameter: {},isLoading: false,isToken: false);
  237. if(dict!=null){
  238. _tokenStr = dict['token'];
  239. _urlStr = dict['prefix'];
  240. }
  241. }
  242. _onUpload(var value) async {
  243. if (value == null) {
  244. return;
  245. }
  246. print(value.path);
  247. print(_tokenStr);
  248. final syStorage = new SyFlutterQiniuStorage();
  249. //监听上传进度
  250. syStorage.onChanged().listen((dynamic percent) {
  251. print(percent);
  252. });
  253. //上传文件
  254. var result = await syStorage.upload(value.path, _tokenStr, DateTime.now().millisecondsSinceEpoch.toString() +'.' +value.path.split('.').last);
  255. if(result.success==true){
  256. setState(() {
  257. widget.isPhoto==true?_images.add(_urlStr+'${result.result['key']}'):_videoPath = _urlStr+'${result.result['key']}';
  258. });
  259. }
  260. print(result);
  261. }
  262. // //取消上传
  263. // _onCancel() {
  264. // SyFlutterQiniuStorage.cancelUpload();
  265. // }
  266. //0为视频,1为文章
  267. _postAddSquareData() async{
  268. if(widget.isPhoto==true&&_images.length==0){
  269. ysFlutterToast(context, '请添加图片');
  270. return;
  271. }
  272. if(widget.isPhoto==false&&_videoPath.isEmpty){
  273. ysFlutterToast(context, '请添加视频');
  274. return;
  275. }
  276. if(_title.text.isEmpty){
  277. ysFlutterToast(context, '请输入标题');
  278. return;
  279. }
  280. if(_content.text.isEmpty){
  281. ysFlutterToast(context, '请输入内容');
  282. return;
  283. }
  284. Map request ={};
  285. request['typeId'] = 1;
  286. if(widget.isPhoto==true){
  287. request['type'] = 1;
  288. request['topicTitle'] = _title.text;
  289. request['topicContent'] = _content.text;
  290. request['topicPic'] = _images.join(',');
  291. }else{
  292. request['type'] = 0;
  293. request['videoTitle'] = _title.text;
  294. request['videoContent'] = _content.text;
  295. request['videoPath'] = _videoPath;
  296. }
  297. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/piazza/SquareCommunity/publish',parameter: request,isLoading: true,isToken: true,refresh: (){
  298. _postAddSquareData();
  299. });
  300. if(dict!=null){
  301. Navigator.of(context).pop('');
  302. }
  303. }
  304. }