YSDiary.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'YSAddDiary.dart';
  6. import 'YSPhoto.dart';
  7. class YSDiary extends StatefulWidget {
  8. @override
  9. _YSDiaryState createState() => _YSDiaryState();
  10. }
  11. String timeSwitch(String timeStr){
  12. DateTime time = DateTime.parse(timeStr);
  13. return "${time.day.toString().padLeft(2,'0')}${time.month.toString().padLeft(2,'0')}月";
  14. }
  15. class _YSDiaryState extends State<YSDiary> {
  16. List dataArray = [];
  17. int _page = 1;
  18. ScrollController _scroll = ScrollController();
  19. String _name = '';
  20. String _avatar = '';
  21. @override
  22. void initState() {
  23. Future.delayed(Duration(seconds: 0)).then((value){
  24. this._refreshData();
  25. _scroll.addListener(() {
  26. if(_scroll.position.pixels == _scroll.position.maxScrollExtent){
  27. this._loadMoreData();
  28. }
  29. });
  30. });
  31. Future<SharedPreferences> prefer = SharedPreferences.getInstance();
  32. prefer.then((value){
  33. _name = value.getString('name')??'';
  34. _avatar = value.getString('avatar')??'';
  35. });
  36. super.initState();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return CupertinoPageScaffold(
  41. backgroundColor: Colors.white,
  42. child: Stack(
  43. children: [
  44. Container(
  45. height: 191.5,
  46. width: MediaQuery.of(context).size.width,
  47. child: Image(
  48. image: AssetImage('lib/images/diarybg.png'),
  49. fit: BoxFit.fill,
  50. ),
  51. ),
  52. CupertinoButton(
  53. padding: EdgeInsets.all(0),
  54. child: Container(
  55. margin: EdgeInsets.only(left: 15,top: MediaQuery.of(context).padding.top+5),
  56. child: Icon(Icons.keyboard_arrow_left,size: 30,color: Colors.white,),
  57. ),
  58. onPressed: (){
  59. Navigator.pop(context);
  60. },
  61. ),
  62. Container(
  63. margin: EdgeInsets.only(top: 170),
  64. height: MediaQuery.of(context).size.height-170,
  65. decoration: BoxDecoration(
  66. borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),
  67. color: Colors.white,
  68. ),
  69. child: Column(
  70. children: [
  71. Container(
  72. height: 70,
  73. margin: EdgeInsets.only(top: 80,bottom: 20),
  74. padding: EdgeInsets.only(left: 20,right: 20),
  75. child: Row(
  76. children: [
  77. Text('今天',style: TextStyle(fontSize: 26,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  78. CupertinoButton(
  79. padding: EdgeInsets.all(0),
  80. child: Container(
  81. margin: EdgeInsets.only(left: 20),
  82. height: 70,
  83. width: 70,
  84. color: Color(0xFFEFEFEF),
  85. child: Icon(Icons.photo_camera,size: 35,color: Color(0xFF8A8A8A),),
  86. ),
  87. onPressed: (){
  88. Navigator.of(context).push(
  89. CupertinoPageRoute(
  90. builder: (context){
  91. return YSAddDiary();
  92. }
  93. )
  94. ).then((value){
  95. if(value!=null){
  96. _refreshData();
  97. }
  98. });
  99. },
  100. )
  101. ],
  102. ),
  103. ),
  104. Container(
  105. height: MediaQuery.of(context).size.height-340,
  106. child: RefreshIndicator(
  107. onRefresh: _refreshData,
  108. child: dataArray.length==0?Container(
  109. alignment: Alignment.center,
  110. child: Image.asset('lib/images/none.png',height: 200,width: 200,),
  111. ):ListView.builder(itemBuilder: (context,index){
  112. return Container(
  113. padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
  114. child: Row(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: [
  117. RichText(
  118. text: TextSpan(
  119. text: timeSwitch(dataArray[index]['created_at']).substring(0,2),
  120. style: TextStyle(fontSize: 26,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),
  121. children: [
  122. TextSpan(
  123. text: ' '+timeSwitch(dataArray[index]['created_at']).substring(2,timeSwitch(dataArray[index]['created_at']).length),
  124. style: TextStyle(fontSize: 13),
  125. )
  126. ]
  127. ),
  128. ),
  129. Container(
  130. width: MediaQuery.of(context).size.width-150,
  131. padding: EdgeInsets.only(left: 15,right: 15,top: 5),
  132. child: Column(
  133. children: [
  134. dataArray[index]['body']!=null?Text(dataArray[index]['body'],
  135. style: TextStyle(fontSize: 12,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(),
  136. Container(
  137. margin: EdgeInsets.only(top: dataArray[index]['body']!=null?5:0),
  138. height: dataArray[index]['covers'].length==0?0:dataArray[index]['covers'].length%3!=0?70.0*(((dataArray[index]['covers'].length)~/3)+1):70.0*((dataArray[index]['covers'].length)~/3),
  139. width: MediaQuery.of(context).size.width-150,
  140. child: GridView.builder(
  141. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  142. crossAxisCount: 3,
  143. ),
  144. itemBuilder: (context,indexSub){
  145. return GestureDetector(
  146. onTap: (){
  147. Navigator.of(context).push(
  148. CupertinoPageRoute(builder: (context){
  149. return YSPhotoDetail(url: dataArray[index]['covers'][indexSub],);
  150. })
  151. );
  152. },
  153. child: Container(
  154. height: 70,
  155. width: 70,
  156. margin: EdgeInsets.only(right: 5,top: 5,bottom: 5),
  157. color: Color(0xFFEFEFEF),
  158. child: Image(image: NetworkImage('${dataArray[index]['covers'][indexSub]}'),fit: BoxFit.fill,)
  159. ),
  160. );
  161. },
  162. itemCount: dataArray[index]['covers'].length,
  163. physics: NeverScrollableScrollPhysics(),
  164. padding: EdgeInsets.all(0),
  165. ),
  166. ),
  167. Container(
  168. child: Row(
  169. children: [
  170. dataArray[index]['tag']!=null?Text('#'+dataArray[index]['tag']+'#',
  171. style: TextStyle(fontSize: 12,color: Color(0xFFDB5278),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(),
  172. dataArray[index]['position']!=null?
  173. Text('${dataArray[index]['position']}',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(),
  174. ],
  175. ),
  176. )
  177. ],
  178. crossAxisAlignment: CrossAxisAlignment.start,
  179. ),
  180. ),
  181. GestureDetector(
  182. child: Container(
  183. height: 20,
  184. width: 20,
  185. child: Image.asset('lib/images/off2.png'),
  186. ),
  187. onTap: (){
  188. showDialog(
  189. context:context,
  190. builder:(context){
  191. return AlertDialog(
  192. title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  193. content: Text('确认删除此条记录?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  194. actions: [
  195. CupertinoButton(
  196. padding: EdgeInsets.all(0),
  197. child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  198. onPressed: (){
  199. Navigator.pop(context);
  200. },
  201. ),
  202. CupertinoButton(
  203. padding: EdgeInsets.all(0),
  204. child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  205. onPressed: (){
  206. Navigator.pop(context);
  207. _deleteDiaryData(index);
  208. },
  209. ),
  210. ],
  211. );
  212. }
  213. );
  214. },
  215. )
  216. ],
  217. ),
  218. );
  219. },itemCount: dataArray.length,padding: EdgeInsets.all(0),controller: _scroll,physics: AlwaysScrollableScrollPhysics(),),
  220. ),
  221. )
  222. ],
  223. ),
  224. ),
  225. Container(
  226. margin: EdgeInsets.only(top: 130,left: MediaQuery.of(context).size.width-80),
  227. height: 70,
  228. width: 70,
  229. decoration: BoxDecoration(
  230. color: Colors.white,
  231. borderRadius: BorderRadius.all(Radius.circular(35)),
  232. border: Border.all(color: Colors.white,width: 2),
  233. image: DecorationImage(image: _avatar==''?AssetImage('lib/images/avtar.png'):NetworkImage(_avatar),fit: BoxFit.fill)
  234. ),
  235. ),
  236. Container(
  237. margin: EdgeInsets.only(top: 205,left: 15,right: 15),
  238. width: MediaQuery.of(context).size.width-30,
  239. height: 20,
  240. alignment: Alignment.centerRight,
  241. child: Text(_name,style: TextStyle(fontSize: 14,color: Color(0xFF292929),fontWeight: FontWeight.normal,decoration: TextDecoration.none),overflow: TextOverflow.ellipsis,),
  242. )
  243. ],
  244. ),
  245. );
  246. }
  247. Future<void> _refreshData() async{
  248. SharedPreferences prefer = await SharedPreferences.getInstance();
  249. int chapter = prefer.getInt('chapters')??0;
  250. _page = 1;
  251. Map request = Map();
  252. request['category_id'] = chapter+1;
  253. request['page'] = _page;
  254. if(chapter>1){
  255. request['child_id'] = prefer.getInt('childId');
  256. }
  257. Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'diary/diary', request);
  258. if(dict!=null){
  259. setState(() {
  260. dataArray = dict['data']['datas']??[];
  261. });
  262. }
  263. }
  264. Future<void> _loadMoreData() async{
  265. SharedPreferences prefer = await SharedPreferences.getInstance();
  266. int chapter = prefer.getInt('chapters')??0;
  267. _page++;
  268. Map request = Map();
  269. request['category_id'] = 1;
  270. request['page'] = _page;
  271. if(chapter>1){
  272. request['child_id'] = prefer.getInt('childId');
  273. }
  274. Map dict = await ysRequestHttp(context, requestType.post, 'diary/diary', request);
  275. if(dict!=null){
  276. setState(() {
  277. dataArray.addAll(dict['data']['datas']??[]);
  278. });
  279. }
  280. }
  281. _deleteDiaryData(int index) async{
  282. Map dict = await ysRequestHttp(context, requestType.delete, 'diary/delete', {'id':dataArray[index]['id']});
  283. if(dict!=null){
  284. setState(() {
  285. dataArray.removeAt(index);
  286. });
  287. }
  288. }
  289. }