YSSquare.dart 24 KB

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