YSAddDiary.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
  4. import 'package:flutterappfuyou/code/base/YSTools.dart';
  5. import 'package:image_picker/image_picker.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'base/YSBase.dart';
  8. class YSAddDiary extends StatefulWidget {
  9. @override
  10. _YSAddDiaryState createState() => _YSAddDiaryState();
  11. }
  12. class _YSAddDiaryState extends State<YSAddDiary> {
  13. int selectedIndex = 9999;
  14. String tagStr,locationStr;
  15. List tags = [];
  16. final ImagePicker _picker = ImagePicker();
  17. List images = [];
  18. List paths = [];
  19. TextEditingController _content = TextEditingController();
  20. @override
  21. void initState() {
  22. Future.delayed(Duration(seconds: 0)).then((value){
  23. _getTagListData();
  24. });
  25. super.initState();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return YSBase(
  30. ystitle: '新增日记',
  31. ysright: CupertinoButton(
  32. padding: EdgeInsets.all(0),
  33. child: Container(
  34. height: 30,
  35. width: 55,
  36. decoration: BoxDecoration(
  37. color: Color(0xFFFFEB3B),
  38. border: Border.all(color: Color(0xFF292929),width: 1),
  39. borderRadius: BorderRadius.all(Radius.circular(20))
  40. ),
  41. alignment: Alignment.center,
  42. child: Text('发表',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),),
  43. ),
  44. onPressed: (){
  45. _postDiaryData();
  46. },
  47. ),
  48. yschild: Container(
  49. color: Colors.white,
  50. child: SingleChildScrollView(
  51. child: Column(
  52. children: [
  53. Container(
  54. width: MediaQuery.of(context).size.width,
  55. height: 120,
  56. margin: EdgeInsets.all(15),
  57. color: Colors.white,
  58. child: CupertinoTextField(
  59. placeholder: '记录生活的点点滴滴',
  60. style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),
  61. maxLines: 30,
  62. decoration: BoxDecoration(),
  63. placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none),
  64. controller: _content,
  65. ),
  66. ),
  67. Container(
  68. margin: EdgeInsets.only(left: 15,right: 15,bottom: 50),
  69. height: (MediaQuery.of(context).size.width-42)/3,
  70. child: GridView.builder(
  71. scrollDirection: Axis.horizontal,
  72. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1,mainAxisSpacing: 6),
  73. itemBuilder: (context,index){
  74. return GestureDetector(
  75. onTap: (){
  76. if(index<images.length){
  77. }else{
  78. showCupertinoModalPopup(
  79. context: context,
  80. builder: (context) {
  81. return CupertinoActionSheet(
  82. actions: <Widget>[
  83. CupertinoActionSheetAction(
  84. child: Text('相机'),
  85. onPressed: () {
  86. Navigator.pop(context);
  87. _picker.getImage(source: ImageSource.camera,imageQuality: 50).then((value) => {
  88. _uploadImageData(value)
  89. });
  90. },
  91. ),
  92. CupertinoActionSheetAction(
  93. child: Text('相册'),
  94. onPressed: () {
  95. Navigator.pop(context);
  96. _picker.getImage(source: ImageSource.gallery,imageQuality: 50).then((value) => {
  97. _uploadImageData(value)
  98. });
  99. },
  100. ),
  101. ],
  102. cancelButton: CupertinoActionSheetAction(
  103. child: Text('取消'),
  104. onPressed: () {
  105. Navigator.pop(context);
  106. },
  107. ),
  108. );
  109. }
  110. );
  111. }
  112. },
  113. child: index==images.length?Container(
  114. height: (MediaQuery.of(context).size.width-42)/3,
  115. width: (MediaQuery.of(context).size.width-42)/3,
  116. color: Color(0xFFEFEFEF),
  117. child: Icon(Icons.add,size: 40,color: Color(0xFF8A8A8A),),
  118. ):Container(
  119. height: (MediaQuery.of(context).size.width-42)/3,
  120. width: (MediaQuery.of(context).size.width-42)/3,
  121. color: Color(0xFFEFEFEF),
  122. child: Image(image: NetworkImage('${images[index]}'),fit: BoxFit.fill,)
  123. ),
  124. );
  125. },
  126. itemCount: images.length+1,
  127. ),
  128. ),
  129. Container(
  130. width: MediaQuery.of(context).size.width,
  131. height: 1.5,
  132. child: Image.asset('lib/images/line.png'),
  133. ),
  134. CupertinoButton(
  135. padding: EdgeInsets.all(0),
  136. child: Container(
  137. height: 50,
  138. width: MediaQuery.of(context).size.width,
  139. margin: EdgeInsets.only(left: 15,right: 15),
  140. child: Row(
  141. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  142. children: [
  143. Row(
  144. children: [
  145. Container(width: 20,height: 20,margin: EdgeInsets.only(right: 15,left: 30),child: Image.asset('lib/images/tag.png'),),
  146. Text(tagStr==null?'添加标签':tagStr,style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),)
  147. ],
  148. ),
  149. Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFFAEB3BD),)
  150. ],
  151. ),
  152. ),
  153. onPressed: (){
  154. showModalBottomSheet(
  155. context: context,
  156. backgroundColor: Colors.transparent,
  157. builder: (context){
  158. return StatefulBuilder(
  159. builder: (context,setYSState){
  160. return Container(
  161. height: 274,
  162. child: Column(
  163. crossAxisAlignment: CrossAxisAlignment.end,
  164. children: [
  165. CupertinoButton(
  166. padding: EdgeInsets.all(0),
  167. child: Container(
  168. height: 20,
  169. child: Image.asset('lib/images/off.png'),
  170. ),
  171. onPressed: (){
  172. Navigator.pop(context);
  173. },
  174. ),
  175. Container(
  176. height: 230,
  177. decoration: BoxDecoration(
  178. color: Colors.white,
  179. borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
  180. ),
  181. child: Column(
  182. children: [
  183. Container(
  184. width: MediaQuery.of(context).size.width,
  185. padding: EdgeInsets.only(left: 15,right: 15),
  186. height: 48.5,
  187. child: Row(
  188. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  189. children: [
  190. Text('选择标签',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),),
  191. CupertinoButton(
  192. padding: EdgeInsets.all(0),
  193. child: Text('完成',style: TextStyle(fontSize: 14,color: Color(0xFF4CC17C),decoration: TextDecoration.none),),
  194. onPressed: (){
  195. Navigator.pop(context);
  196. setState(() {
  197. if(selectedIndex==tags.length){
  198. getStringWidget(1);
  199. }else{
  200. tagStr = tags[selectedIndex]['name'];
  201. }
  202. });
  203. },
  204. )
  205. ],
  206. ),
  207. ),
  208. Container(
  209. width: MediaQuery.of(context).size.width,
  210. height: 1.5,
  211. child: Image.asset('lib/images/line.png'),
  212. ),
  213. Container(
  214. width: MediaQuery.of(context).size.width,
  215. padding: EdgeInsets.only(left: 15,right: 15,top: 20,bottom: 20),
  216. height: 180,
  217. child: GridView.builder(
  218. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3,crossAxisSpacing: 20,mainAxisSpacing: 20,childAspectRatio: 0.3),
  219. itemBuilder: (context,index){
  220. return GestureDetector(
  221. onTap: (){
  222. if(index==tags.length){
  223. Navigator.pop(context);
  224. getStringWidget(1);
  225. }else{
  226. setYSState(() {
  227. selectedIndex = index;
  228. });
  229. }
  230. },
  231. child: Container(
  232. padding: EdgeInsets.all(5),
  233. alignment: Alignment.center,
  234. child: Text(tags.length==index?'自定义':tags[index]['name'],style: TextStyle(fontSize: 14,color: selectedIndex==index?Color(0xFFFF6688):Color(0xFF292929),
  235. decoration: TextDecoration.none),maxLines: 1,textAlign: TextAlign.center,),
  236. decoration: BoxDecoration(
  237. borderRadius: BorderRadius.all(Radius.circular(3)),
  238. border: Border.all(color: selectedIndex==index?Color(0xFFFF6688):Color(0xFFB3B3B3),width: 0.5)
  239. ),
  240. ),
  241. );
  242. },
  243. scrollDirection: Axis.horizontal,
  244. itemCount: tags.length+1,
  245. ),
  246. )
  247. ],
  248. ),
  249. )
  250. ],
  251. ),
  252. );
  253. },
  254. );
  255. }
  256. );
  257. },
  258. ),
  259. Container(
  260. width: MediaQuery.of(context).size.width-90,
  261. margin: EdgeInsets.only(left: 65),
  262. height: 1.5,
  263. child: Image.asset('lib/images/line.png'),
  264. ),
  265. CupertinoButton(
  266. padding: EdgeInsets.all(0),
  267. child: Container(
  268. height: 50,
  269. width: MediaQuery.of(context).size.width,
  270. margin: EdgeInsets.only(left: 15,right: 15),
  271. child: Row(
  272. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  273. children: [
  274. Row(
  275. children: [
  276. Container(width: 20,height: 20,margin: EdgeInsets.only(right: 15,left: 30),child: Image.asset('lib/images/location.png'),),
  277. Text(locationStr==null?'添加位置':locationStr,style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none),)
  278. ],
  279. ),
  280. Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFFAEB3BD),)
  281. ],
  282. ),
  283. ),
  284. onPressed: (){
  285. getStringWidget(2);
  286. },
  287. ),
  288. Container(
  289. width: MediaQuery.of(context).size.width-90,
  290. margin: EdgeInsets.only(left: 65),
  291. height: 1.5,
  292. child: Image.asset('lib/images/line.png'),
  293. ),
  294. ],
  295. ),
  296. ),
  297. ),
  298. );
  299. }
  300. _getTagListData() async{
  301. SharedPreferences prefer = await SharedPreferences.getInstance();
  302. var list = await ysRequestHttpNoLoading(context, requestType.get, 'basic/Tag', {'category_id':prefer.getInt('chapters')+1});
  303. if(list!=null){
  304. tags = list;
  305. }
  306. }
  307. _uploadImageData(var image) async{
  308. ysUploadFile(context,path: image.path,type: 'diary',setter: (value){
  309. paths.add('${value['path']}');
  310. images.add('${value['url']}');
  311. setState(() {});
  312. });
  313. // var dict = await ysRequestHttpNoLoading(context, requestType.get, 'upQiniuToken', {'type':'diary'});
  314. // if(dict!=null){
  315. // var result = await syStorage.upload('${image.path}', '${dict['token']}', '${dict['path']}');
  316. // if(result.success==true){
  317. // paths.add('${dict['path']}');
  318. // setState(() {
  319. // images.add('${dict['url']}');
  320. // });
  321. // }
  322. // }
  323. }
  324. _postDiaryData() async{
  325. if(images.length==0&&_content.text==null){
  326. ysFlutterToast(context, '请添加内容信息');
  327. return;
  328. }
  329. Map request = Map();
  330. SharedPreferences prefer = await SharedPreferences.getInstance();
  331. int chapter = prefer.getInt('chapters')+1;
  332. request['category_id'] = chapter;
  333. if(chapter>2){
  334. request['child_id'] = prefer.getInt('childId');
  335. }
  336. if(_content.text!=null){
  337. request['body'] = _content.text;
  338. }
  339. if(paths.length>0){
  340. request['covers'] = paths;
  341. }
  342. if(locationStr!=null){
  343. request['position'] = locationStr;
  344. }
  345. if(tagStr!=null){
  346. request['tag'] = tagStr;
  347. }
  348. Map dict = await ysRequestHttp(context, requestType.post, 'diary/diarize', request);
  349. if(dict!=null){
  350. Navigator.of(context).pop('确定');
  351. }
  352. }
  353. getStringWidget(int type){
  354. TextEditingController textController = TextEditingController();
  355. showDialog(context: context,builder: (context){
  356. return Dialog(
  357. backgroundColor: Colors.transparent,
  358. child: StatefulBuilder(
  359. builder: (context,setYSState){
  360. return Container(
  361. width: MediaQuery.of(context).size.width,
  362. padding: EdgeInsets.all(15),
  363. height: 150,
  364. decoration: BoxDecoration(
  365. color: Colors.white,
  366. borderRadius: BorderRadius.all(Radius.circular(10))
  367. ),
  368. child: Column(
  369. crossAxisAlignment: CrossAxisAlignment.start,
  370. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  371. children: [
  372. Text(type==1?'添加标签':'添加位置',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  373. Container(
  374. height: 30,
  375. width: MediaQuery.of(context).size.width-30,
  376. child: CupertinoTextField(
  377. style: TextStyle(fontSize: 13,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  378. placeholderStyle: TextStyle(fontSize: 13,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  379. placeholder: type==1?'请输入标签':'请输入位置',
  380. controller: textController,
  381. decoration: BoxDecoration(),
  382. ),
  383. //Text('chooseDate',style: TextStyle(fontSize: 13,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  384. decoration: BoxDecoration(
  385. border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFD7D7D7)))
  386. ),
  387. ),
  388. Row(
  389. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  390. children: [
  391. Container(),
  392. Row(
  393. children: [
  394. CupertinoButton(
  395. padding: EdgeInsets.all(0),
  396. child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  397. onPressed: (){
  398. Navigator.pop(context);
  399. },
  400. ),
  401. CupertinoButton(
  402. padding: EdgeInsets.all(0),
  403. child: Text('确定',style: TextStyle(fontSize: 13,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  404. onPressed: (){
  405. Navigator.pop(context);
  406. if(textController.value.text.length>0){
  407. setState(() {
  408. if(type==1){
  409. tagStr = textController.value.text;
  410. }else{
  411. locationStr = textController.value.text;
  412. }
  413. });
  414. }
  415. },
  416. )
  417. ],
  418. )
  419. ],
  420. )
  421. ],
  422. ),
  423. );
  424. },
  425. ),
  426. );
  427. });
  428. }
  429. }