YSCollection.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter_easyrefresh/easy_refresh.dart';
  5. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  6. import 'package:ysairplane/base/YSBase.dart';
  7. import 'package:ysairplane/tools/YSNetWorking.dart';
  8. import 'YSAuthorSquare.dart';
  9. import 'YSSquareArticle.dart';
  10. import 'YSSquareVideo.dart';
  11. class YSCollection extends StatefulWidget {
  12. @override
  13. _YSCollectionState createState() => _YSCollectionState();
  14. }
  15. class _YSCollectionState extends State<YSCollection> {
  16. List _dataArray = [];
  17. int _page = 1;
  18. ScrollController _scrollController = ScrollController();
  19. @override
  20. void initState() {
  21. Future.delayed(Duration(seconds: 0)).then((value){
  22. _refreshData();
  23. });
  24. super.initState();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return YSBase(
  29. ystitle: '收藏',
  30. yschild: Container(
  31. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44,
  32. width: MediaQuery.of(context).size.width,
  33. color: Color(0xFFF5F6F8),
  34. child: EasyRefresh(
  35. onRefresh: _refreshData,
  36. onLoad: _loadMore,
  37. header: TaurusHeader(
  38. ),
  39. footer: TaurusFooter(
  40. ),
  41. child: StaggeredGridView.countBuilder(
  42. controller: _scrollController,
  43. crossAxisCount: 4,
  44. crossAxisSpacing: 8,
  45. mainAxisSpacing: 8,
  46. itemCount: _dataArray.length,
  47. itemBuilder: (context, index) {
  48. return GestureDetector(
  49. onTap: (){
  50. Navigator.of(context,rootNavigator: true).push(
  51. CupertinoPageRoute(
  52. builder: (context){
  53. return _dataArray[index]['type']==1?YSSquareArticle(squareId: _dataArray[index]['id'],typeId: _dataArray[index]['type'],)
  54. :YSSquareVideo(squareId: _dataArray[index]['id'],typeId: _dataArray[index]['type'],);
  55. }
  56. )
  57. );
  58. },
  59. child: Stack(
  60. children: [
  61. Container(
  62. color: Colors.white,
  63. child:Column(
  64. children: [
  65. AspectRatio(
  66. child: Container(
  67. decoration: BoxDecoration(
  68. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
  69. ),
  70. child: ClipRRect(
  71. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
  72. child: CachedNetworkImage(
  73. imageUrl: _dataArray[index]['type']==0?'${_dataArray[index]['coverPath']}':'${_dataArray[index]['topicCover']}',
  74. fit: BoxFit.fill
  75. ),
  76. ),
  77. ),
  78. aspectRatio: double.parse('${_dataArray[index]['coverWidth']}')/double.parse('${_dataArray[index]['coverHeight']}')
  79. ),
  80. Container(
  81. child: Text(_dataArray[index]['type']==0?'${_dataArray[index]['videoTitle']}':'${_dataArray[index]['topicTitle']}',
  82. style: TextStyle(fontSize: 13,color: Color(0xFF333333),decoration: TextDecoration.none,),maxLines: 2,overflow: TextOverflow.ellipsis,),
  83. padding: EdgeInsets.all(5),
  84. alignment: Alignment.centerLeft,
  85. ),
  86. Container(
  87. child: Row(
  88. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  89. children: [
  90. GestureDetector(
  91. behavior: HitTestBehavior.opaque,
  92. child: Row(
  93. children: [
  94. Container(
  95. height: 20,
  96. width: 20,
  97. margin: EdgeInsets.only(right: 5),
  98. decoration: BoxDecoration(
  99. color: Colors.black12,
  100. borderRadius: BorderRadius.all(Radius.circular(10)),
  101. ),
  102. child: ClipRRect(
  103. borderRadius: BorderRadius.all(Radius.circular(10)),
  104. child: CachedNetworkImage(
  105. imageUrl: '${_dataArray[index]['user']['avatar']}',
  106. fit: BoxFit.fill,
  107. ),
  108. ),
  109. ),
  110. Container(
  111. width: 70,
  112. child: Text('${_dataArray[index]['user']['nickname']}',
  113. style: TextStyle(fontSize: 12,color: Color(0xFF333333),decoration: TextDecoration.none,),overflow: TextOverflow.ellipsis,),
  114. )
  115. ],
  116. ),
  117. onTap: (){
  118. Navigator.of(context,rootNavigator: true).push(
  119. CupertinoPageRoute(
  120. builder: (context){
  121. return YSAuthorSquare(authorId: int.parse('${_dataArray[index]['user']['id']}'),);
  122. }
  123. )
  124. );
  125. },
  126. ),
  127. GestureDetector(
  128. behavior: HitTestBehavior.opaque,
  129. child: Row(
  130. children: [
  131. Container(
  132. height: 15,
  133. width: 15,
  134. margin: EdgeInsets.only(right: 5),
  135. child: Image.asset(_dataArray[index]['likeStatus']==0?'lib/images/love.png':'lib/images/love2.png')
  136. ),
  137. Container(
  138. child: Text('${_dataArray[index]['likeCounts']}',style: TextStyle(fontSize: 12,color: Color(0xFFEE564C),decoration: TextDecoration.none,fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,),
  139. )
  140. ],
  141. mainAxisSize: MainAxisSize.min,
  142. ),
  143. onTap: (){
  144. _postAgreementData(index);
  145. },
  146. ),
  147. ],
  148. ),
  149. padding: EdgeInsets.only(left: 5,right: 5,top: 3,bottom: 5),
  150. ),
  151. ],
  152. ),
  153. ),
  154. Container(
  155. height: 15,
  156. margin: EdgeInsets.only(top: 10,left: 10),
  157. decoration: BoxDecoration(
  158. color: Colors.black12,
  159. borderRadius: BorderRadius.all(Radius.circular(7.5)),
  160. ),
  161. child: Row(
  162. mainAxisSize: MainAxisSize.min,
  163. children: [
  164. Container(
  165. height: 10,
  166. width: 15,
  167. margin: EdgeInsets.only(right: 3),
  168. child: Image.asset('lib/images/eyes2.png')
  169. ),
  170. Container(
  171. child: Text('${_dataArray[index]['likeCounts']}',style: TextStyle(fontSize: 12,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,),
  172. )
  173. ],
  174. ),
  175. padding: EdgeInsets.only(left: 8,right: 8),
  176. ),
  177. _dataArray[index]['type']==0?Container(
  178. margin: EdgeInsets.only(left: (MediaQuery.of(context).size.width-25)/2-30,
  179. top: (MediaQuery.of(context).size.width-25)/2/(double.parse('${_dataArray[index]['coverWidth']}')/double.parse('${_dataArray[index]['coverHeight']}'))-30),
  180. height: 20,
  181. width: 20,
  182. child: Icon(Icons.play_circle_filled,color: Colors.white,),
  183. ):Container()
  184. ],
  185. ),
  186. );
  187. },
  188. staggeredTileBuilder: (index) =>StaggeredTile.fit(2),
  189. padding: EdgeInsets.only(top: 10),
  190. ),
  191. ),
  192. ),
  193. );
  194. }
  195. Future<void> _refreshData() async{
  196. _page = 1;
  197. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/piazza/SquareCommunity/myCollect',parameter: {'pageNo':_page,'pageSize':10},isToken: true,refresh: (){
  198. _refreshData();
  199. });
  200. if(dict!=null){
  201. setState(() {
  202. _dataArray = dict['data']['resultList'];
  203. });
  204. }
  205. }
  206. Future<void> _loadMore() async{
  207. _page++;
  208. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/piazza/SquareCommunity/myCollect',parameter: {'pageNo':_page,'pageSize':10},isToken: true,refresh: (){
  209. _loadMore();
  210. });
  211. if(dict!=null){
  212. setState(() {
  213. _dataArray.addAll(dict['data']['resultList']);
  214. });
  215. }
  216. }
  217. _postAgreementData(int index) async{
  218. Map request = {'type':1};
  219. if(_dataArray[index]['type']==0){
  220. request['squareId'] = _dataArray[index]['id'];
  221. }else{
  222. request['topicId'] = _dataArray[index]['id'];
  223. }
  224. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/piazza/SquareCommunity/likeAndCollect',parameter: request,isLoading: false,isToken: true,refresh: (){
  225. });
  226. if(dict!=null){
  227. setState(() {
  228. _dataArray[index]['likeStatus'] = dict['data'];
  229. if(dict['data'] == 0){
  230. _dataArray[index]['likeCounts'] = int.parse('${_dataArray[index]['likeCounts']}') - 1;
  231. }else{
  232. _dataArray[index]['likeCounts'] = int.parse('${_dataArray[index]['likeCounts']}') + 1;
  233. }
  234. });
  235. }
  236. }
  237. }