YSSquare.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/code/YSAddSquare.dart';
  7. import 'package:ysairplane/code/YSMineSquare.dart';
  8. import 'package:ysairplane/code/YSSquareArticle.dart';
  9. import 'package:ysairplane/code/YSSquareVideo.dart';
  10. import 'package:ysairplane/tools/YSNetWorking.dart';
  11. import 'package:ysairplane/tools/YSTools.dart';
  12. import 'package:shared_preferences/shared_preferences.dart';
  13. import 'YSAuthorSquare.dart';
  14. class YSSquare extends StatefulWidget {
  15. @override
  16. _YSSquareState createState() => _YSSquareState();
  17. }
  18. class _YSSquareState extends State<YSSquare> with AutomaticKeepAliveClientMixin{
  19. ScrollController _scrollController = ScrollController();
  20. List _titles = [];
  21. List _dataArray = [];
  22. int _selected = 0;
  23. int _page = 1;
  24. String _name='';
  25. String _avatar;
  26. @override
  27. bool get wantKeepAlive => true;
  28. @override
  29. void initState() {
  30. Future.delayed(Duration(seconds: 0)).then((value){
  31. _getSquareTitleData();
  32. });
  33. super.initState();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. super.build(context);
  38. return CupertinoPageScaffold(
  39. child: Stack(
  40. children: [
  41. SingleChildScrollView(
  42. child: Column(
  43. children: [
  44. Container(
  45. height: MediaQuery.of(context).padding.top+55,
  46. padding: EdgeInsets.only(left: 15,right: 15,top: MediaQuery.of(context).padding.top+5),
  47. color: Colors.white,
  48. child: Row(
  49. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  50. children: [
  51. GestureDetector(
  52. onTap: (){
  53. Navigator.of(context,rootNavigator: true).push(
  54. CupertinoPageRoute(
  55. builder: (context){
  56. return YSMineSquare();
  57. }
  58. )
  59. );
  60. },
  61. child: Container(
  62. width: (MediaQuery.of(context).size.width-30)/3,
  63. child: Row(
  64. children: [
  65. Container(
  66. height: 30,
  67. width: 30,
  68. margin: EdgeInsets.only(right: 5),
  69. decoration: BoxDecoration(
  70. color: Colors.black12,
  71. borderRadius: BorderRadius.all(Radius.circular(15))
  72. ),
  73. ),
  74. Container(
  75. width: 70,
  76. child: Text(_name,style: TextStyle(fontSize: 15,color: Color(0xFF333333),decoration: TextDecoration.none,),overflow: TextOverflow.ellipsis,),
  77. )
  78. ],
  79. ),
  80. ),
  81. ),
  82. Container(width: (MediaQuery.of(context).size.width-30)/3,child: Text('广场',style: TextStyle(fontSize: 20,color: Color(0xFF333333),decoration: TextDecoration.none),),alignment: Alignment.center,),
  83. Container(width: (MediaQuery.of(context).size.width-30)/3,child: Icon(Icons.search,size: 25,color: Color(0xFF333333),),alignment: Alignment.centerRight,)
  84. ],
  85. ),
  86. ),
  87. Divider(height: 0.5,color: Color(0xFFEEEEEE),),
  88. Container(
  89. height: 50,
  90. color: Colors.white,
  91. padding: EdgeInsets.only(left: 15,right: 15,top: 20),
  92. child: ListView.separated(
  93. scrollDirection: Axis.horizontal,
  94. itemBuilder: (context,index){
  95. return GestureDetector(
  96. onTap: (){
  97. setState(() {
  98. _selected = index;
  99. });
  100. _refreshData();
  101. },
  102. child: Container(
  103. height: 30,
  104. child: Column(
  105. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  106. crossAxisAlignment: CrossAxisAlignment.start,
  107. children: [
  108. Text('${_titles[index]['name']}',style: TextStyle(fontSize: 16,color: index==_selected?Color(0xFF007AFF):Color(0xFF333333),decoration: TextDecoration.none,fontWeight: index==_selected?FontWeight.bold:FontWeight.normal),),
  109. Container(height: 2,width: 30,color: index==_selected?Color(0xFF007AFF):Colors.transparent,)
  110. ],
  111. ),
  112. ),
  113. );
  114. },
  115. separatorBuilder: (context,index){
  116. return Container(width: 20,);
  117. },
  118. itemCount: _titles.length
  119. ),
  120. ),
  121. Container(
  122. margin: EdgeInsets.only(left: 10,right: 10),
  123. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-155,
  124. child: EasyRefresh(
  125. onRefresh: _refreshData,
  126. onLoad: _loadMore,
  127. header: TaurusHeader(
  128. ),
  129. footer: TaurusFooter(
  130. ),
  131. child: StaggeredGridView.countBuilder(
  132. controller: _scrollController,
  133. crossAxisCount: 4,
  134. crossAxisSpacing: 8,
  135. mainAxisSpacing: 8,
  136. itemCount: _dataArray.length,
  137. itemBuilder: (context, index) {
  138. return GestureDetector(
  139. onTap: (){
  140. Navigator.of(context,rootNavigator: true).push(
  141. CupertinoPageRoute(
  142. builder: (context){
  143. return _dataArray[index]['type']==1?YSSquareArticle(squareId: _dataArray[index]['id'],typeId: _dataArray[index]['type'],)
  144. :YSSquareVideo(squareId: _dataArray[index]['id'],typeId: _dataArray[index]['type'],);
  145. }
  146. )
  147. );
  148. },
  149. child: Stack(
  150. children: [
  151. Container(
  152. color: Colors.white,
  153. child:Column(
  154. children: [
  155. AspectRatio(
  156. child: Container(
  157. decoration: BoxDecoration(
  158. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
  159. ),
  160. child: ClipRRect(
  161. borderRadius: BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
  162. child: CachedNetworkImage(
  163. imageUrl: _dataArray[index]['type']==0?'${_dataArray[index]['coverPath']}':'${_dataArray[index]['topicCover']}',
  164. fit: BoxFit.fill
  165. ),
  166. ),
  167. ),
  168. aspectRatio: double.parse('${_dataArray[index]['coverWidth']}')/double.parse('${_dataArray[index]['coverHeight']}')
  169. ),
  170. Container(
  171. child: Text(_dataArray[index]['type']==0?'${_dataArray[index]['videoTitle']}':'${_dataArray[index]['topicTitle']}',
  172. style: TextStyle(fontSize: 13,color: Color(0xFF333333),decoration: TextDecoration.none,),maxLines: 2,overflow: TextOverflow.ellipsis,),
  173. padding: EdgeInsets.all(5),
  174. alignment: Alignment.centerLeft,
  175. ),
  176. Container(
  177. child: Row(
  178. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  179. children: [
  180. GestureDetector(
  181. behavior: HitTestBehavior.opaque,
  182. child: Row(
  183. children: [
  184. Container(
  185. height: 20,
  186. width: 20,
  187. margin: EdgeInsets.only(right: 5),
  188. decoration: BoxDecoration(
  189. color: Colors.black12,
  190. borderRadius: BorderRadius.all(Radius.circular(10)),
  191. ),
  192. child: ClipRRect(
  193. borderRadius: BorderRadius.all(Radius.circular(10)),
  194. child: CachedNetworkImage(
  195. imageUrl: '${_dataArray[index]['user']['avatar']}',
  196. fit: BoxFit.fill,
  197. ),
  198. ),
  199. ),
  200. Container(
  201. width: 70,
  202. child: Text('${_dataArray[index]['user']['nickname']}',
  203. style: TextStyle(fontSize: 12,color: Color(0xFF333333),decoration: TextDecoration.none,),overflow: TextOverflow.ellipsis,),
  204. )
  205. ],
  206. ),
  207. onTap: (){
  208. Navigator.of(context,rootNavigator: true).push(
  209. CupertinoPageRoute(
  210. builder: (context){
  211. return YSAuthorSquare(authorId: int.parse('${_dataArray[index]['user']['id']}'),);
  212. }
  213. )
  214. );
  215. },
  216. ),
  217. GestureDetector(
  218. behavior: HitTestBehavior.opaque,
  219. child: Row(
  220. children: [
  221. Container(
  222. height: 15,
  223. width: 15,
  224. margin: EdgeInsets.only(right: 5),
  225. child: Image.asset(_dataArray[index]['likeStatus']==0?'lib/images/love.png':'lib/images/love2.png')
  226. ),
  227. Container(
  228. child: Text('${_dataArray[index]['likeCounts']}',style: TextStyle(fontSize: 12,color: Color(0xFFEE564C),decoration: TextDecoration.none,fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,),
  229. )
  230. ],
  231. mainAxisSize: MainAxisSize.min,
  232. ),
  233. onTap: (){
  234. _postAgreementData(index);
  235. },
  236. ),
  237. ],
  238. ),
  239. padding: EdgeInsets.only(left: 5,right: 5,top: 3,bottom: 5),
  240. ),
  241. ],
  242. ),
  243. ),
  244. Container(
  245. height: 15,
  246. margin: EdgeInsets.only(top: 10,left: 10),
  247. decoration: BoxDecoration(
  248. color: Colors.black12,
  249. borderRadius: BorderRadius.all(Radius.circular(7.5)),
  250. ),
  251. child: Row(
  252. mainAxisSize: MainAxisSize.min,
  253. children: [
  254. Container(
  255. height: 10,
  256. width: 15,
  257. margin: EdgeInsets.only(right: 3),
  258. child: Image.asset('lib/images/eyes2.png')
  259. ),
  260. Container(
  261. child: Text('${_dataArray[index]['likeCounts']}',style: TextStyle(fontSize: 12,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),overflow: TextOverflow.ellipsis,),
  262. )
  263. ],
  264. ),
  265. padding: EdgeInsets.only(left: 8,right: 8),
  266. ),
  267. _dataArray[index]['type']==0?Container(
  268. margin: EdgeInsets.only(left: (MediaQuery.of(context).size.width-25)/2-30,
  269. top: (MediaQuery.of(context).size.width-25)/2/(double.parse('${_dataArray[index]['coverWidth']}')/double.parse('${_dataArray[index]['coverHeight']}'))-30),
  270. height: 20,
  271. width: 20,
  272. child: Icon(Icons.play_circle_filled,color: Colors.white,),
  273. ):Container()
  274. ],
  275. ),
  276. );
  277. },
  278. staggeredTileBuilder: (index) =>StaggeredTile.fit(2),
  279. padding: EdgeInsets.only(top: 10),
  280. ),
  281. ),
  282. )
  283. ],
  284. ),
  285. padding: EdgeInsets.all(0),
  286. physics: NeverScrollableScrollPhysics(),
  287. ),
  288. GestureDetector(
  289. child: Container(
  290. margin: EdgeInsets.only(top: MediaQuery.of(context).size.height-110,left: MediaQuery.of(context).size.width-60),
  291. height: 50,
  292. width: 50,
  293. child: Image.asset('lib/images/addSquare.png'),
  294. ),
  295. onTap: (){
  296. showModalBottomSheet(
  297. backgroundColor: Colors.transparent,
  298. useRootNavigator: true,
  299. context: context,
  300. builder: (context){
  301. return Container(
  302. height: 200,
  303. decoration: BoxDecoration(
  304. color: Colors.white,
  305. borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
  306. ),
  307. child: Column(
  308. children: [
  309. Container(
  310. height: 44,
  311. padding: EdgeInsets.only(left: 15,right: 15),
  312. child: Row(
  313. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  314. children: [
  315. Container(),
  316. Text('发布',style: TextStyle(fontSize: 15,color: Color(0xFF000000),decoration: TextDecoration.none,fontWeight: FontWeight.bold),),
  317. Icon(Icons.close,size: 20,color: Color(0xFF000000),)
  318. ],
  319. ),
  320. ),
  321. Divider(height: 0.5,thickness: 0.5,color: Color(0x1A000000),),
  322. Container(
  323. height: 155,
  324. child: Row(
  325. children: [
  326. GestureDetector(
  327. child: Container(
  328. width: MediaQuery.of(context).size.width/2-0.25,
  329. height: 60,
  330. child: Column(
  331. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  332. children: [
  333. Image(
  334. image: AssetImage('lib/images/photo.png'),
  335. height: 30,
  336. width: 30
  337. ),
  338. Text('发布图文',style: TextStyle(fontSize: 15,color: Color(0xFF81858C),decoration: TextDecoration.none),),
  339. ],
  340. ),
  341. ),
  342. onTap: (){
  343. Navigator.pop(context);
  344. Navigator.of(context).push(
  345. CupertinoPageRoute(
  346. builder: (context){
  347. return YSAddSquare(isPhoto: true,);
  348. }
  349. )
  350. );
  351. },
  352. ),
  353. Container(
  354. height: 80,
  355. width: 0.5,
  356. color: Color(0x1A000000)
  357. ),
  358. GestureDetector(
  359. onTap: (){
  360. Navigator.pop(context);
  361. Navigator.of(context).push(
  362. CupertinoPageRoute(
  363. builder: (context){
  364. return YSAddSquare(isPhoto: false,);
  365. }
  366. )
  367. ).then((value){
  368. if(value!=null){
  369. _refreshData();
  370. }
  371. });
  372. },
  373. child: Container(
  374. width: MediaQuery.of(context).size.width/2-0.25,
  375. height: 60,
  376. child: Column(
  377. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  378. children: [
  379. Image(
  380. image: AssetImage('lib/images/video.png'),
  381. height: 30,
  382. width: 30
  383. ),
  384. Text('发布视频',style: TextStyle(fontSize: 15,color: Color(0xFF81858C),decoration: TextDecoration.none),),
  385. ],
  386. ),
  387. ),
  388. )
  389. ],
  390. ),
  391. ),
  392. ],
  393. ),
  394. );
  395. }
  396. );
  397. },
  398. )
  399. ],
  400. )
  401. );
  402. }
  403. _getSquareTitleData() async{
  404. SharedPreferences prefer = await SharedPreferences.getInstance();
  405. _name = prefer.getString('name')??'';
  406. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/piazza/SquareCommunity/type/list',parameter: {},isLoading: true,isToken: true,refresh: (){
  407. _getSquareTitleData();
  408. });
  409. if(dict!=null){
  410. setState(() {
  411. _titles = dict['data'];
  412. });
  413. _refreshData();
  414. }
  415. }
  416. Future<void> _refreshData() async{
  417. _page = 1;
  418. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/piazza/SquareCommunity/list',parameter: {'pageNo':_page,'pageSize':10,'id':_titles[_selected]['id']},isToken: true,refresh: (){
  419. _refreshData();
  420. });
  421. if(dict!=null){
  422. setState(() {
  423. _dataArray = dict['data']['resultList'];
  424. });
  425. }
  426. }
  427. Future<void> _loadMore() async{
  428. _page++;
  429. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/piazza/SquareCommunity/list',parameter: {'pageNo':_page,'pageSize':10,'id':_titles[_selected]['id']},isToken: true,refresh: (){
  430. _loadMore();
  431. });
  432. if(dict!=null){
  433. setState(() {
  434. _dataArray.addAll(dict['data']['resultList']);
  435. });
  436. }
  437. }
  438. _postAgreementData(int index) async{
  439. Map request = {'type':1};
  440. if(_dataArray[index]['type']==0){
  441. request['squareId'] = _dataArray[index]['id'];
  442. }else{
  443. request['topicId'] = _dataArray[index]['id'];
  444. }
  445. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/piazza/SquareCommunity/likeAndCollect',parameter: request,isLoading: false,isToken: true,refresh: (){
  446. });
  447. if(dict!=null){
  448. setState(() {
  449. _dataArray[index]['likeStatus'] = dict['data'];
  450. if(dict['data'] == 0){
  451. _dataArray[index]['likeCounts'] = int.parse('${_dataArray[index]['likeCounts']}') - 1;
  452. }else{
  453. _dataArray[index]['likeCounts'] = int.parse('${_dataArray[index]['likeCounts']}') + 1;
  454. }
  455. });
  456. }
  457. }
  458. }