YSAddSquare.dart 13 KB

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