YSSquare.dart 24 KB

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