YSAddSquare.dart 16 KB

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