HomePage.bak.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import 'package:flutter/material.dart';
  2. import '../widget/HomeDrawer.dart';
  3. import 'CreateRoom.dart';
  4. import 'RoomList.dart';
  5. import 'RankList.dart';
  6. import 'RoomInfo.dart';
  7. import 'Recharge.dart';
  8. import 'package:flutter/cupertino.dart';
  9. import 'package:flutter_swiper/flutter_swiper.dart';
  10. import '../model/GameInfoSeasons.dart';
  11. import '../styles/totast.dart';
  12. import '../net/HttpManager.dart';
  13. import '../net/Result.dart';
  14. import 'TipList.dart';
  15. import 'package:flutter_redux/flutter_redux.dart';
  16. import '../redux/AppState.dart';
  17. import 'Setting.dart';
  18. import '../widget/Dialog.dart';
  19. class HomePage extends StatefulWidget {
  20. @override
  21. _HomePageState createState() => _HomePageState();
  22. }
  23. class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  24. List<GameInfoSeasons> seasonList = [];
  25. int nowIndex = 0;
  26. PageController _pageController;
  27. bool showBadge = false;
  28. Future<void> getSeasonInfo() async {
  29. Toast.show(context, '加载中', -1, 'loading');
  30. Result res = await HttpManager.get('gameInfo/seasons');
  31. Toast.hide();
  32. if (res.success) {
  33. List<GameInfoSeasons> list = [];
  34. for (var item in res.data) {
  35. list.add(GameInfoSeasons.fromJson(item));
  36. }
  37. setState(() {
  38. seasonList = list;
  39. });
  40. } else {}
  41. }
  42. void showBackDialog() {
  43. showCustomDialog(context, '暂时没有进行中的房间,敬请期待...');
  44. // showDialog<Null>(
  45. // context: context,
  46. // barrierDismissible: false,
  47. // builder: (BuildContext context) {
  48. // return AlertDialog(
  49. // content: Container(
  50. // height: 50,
  51. // child: Text(
  52. // style: TextStyle(color: Colors.black),
  53. // ),
  54. // ),
  55. // actions: <Widget>[
  56. // FlatButton(
  57. // child: Text('确定'),
  58. // onPressed: () {
  59. // Navigator.of(context).pop();
  60. // },
  61. // ),
  62. // ],
  63. // );
  64. // },
  65. // ).then((val) {});
  66. }
  67. Future<void> getOneRoom() async {
  68. Toast.show(context, '加载中', -1, 'loading');
  69. Result res = await HttpManager.get('houseInfo/getOne', data: {'statusFlag': 0});
  70. Toast.hide();
  71. if (res.success) {
  72. if (res.data != null) {
  73. // Navigator.push(context, CupertinoPageRoute(builder: (context) => RoomInfo(roomId: res.data['id'].toString())));
  74. } else {
  75. showBackDialog();
  76. }
  77. }
  78. }
  79. Future<void> getUnreadMsg() async {
  80. Result res = await HttpManager.get('systemNotice/unread', data: {'userId': StoreProvider.of<AppState>(context).state.userInfo.id, 'statusFlag': 0});
  81. if (res.success && res.data != null) {
  82. if (res.data > 0) {
  83. setState(() {
  84. showBadge = true;
  85. });
  86. } else {
  87. setState(() {
  88. showBadge = false;
  89. });
  90. }
  91. }
  92. }
  93. @override
  94. void initState() {
  95. super.initState();
  96. WidgetsBinding.instance.addObserver(this);
  97. _pageController = PageController(initialPage: 0);
  98. Future.delayed(Duration.zero, () {
  99. getSeasonInfo();
  100. getUnreadMsg();
  101. });
  102. }
  103. @override
  104. void dispose() {
  105. super.dispose();
  106. WidgetsBinding.instance.removeObserver(this);
  107. }
  108. @override
  109. void didChangeAppLifecycleState(AppLifecycleState state) {
  110. if (state == AppLifecycleState.resumed) {
  111. getUnreadMsg();
  112. }
  113. }
  114. @override
  115. Widget build(BuildContext context) {
  116. return Scaffold(
  117. drawer: HomeDrawer(),
  118. body: WillPopScope(
  119. child: Container(
  120. width: double.infinity,
  121. height: double.infinity,
  122. decoration: BoxDecoration(
  123. gradient: LinearGradient(
  124. colors: [Color.fromARGB(255, 177, 59, 56), Color.fromARGB(255, 147, 64, 61)], begin: Alignment.topCenter, end: Alignment.bottomCenter)),
  125. child: SafeArea(
  126. child: centerWidget(context),
  127. ),
  128. ),
  129. onWillPop: () {
  130. Future.value(true);
  131. },
  132. ),
  133. floatingActionButton: floatWidget(),
  134. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  135. );
  136. }
  137. Widget floatWidget() {
  138. return Container(
  139. decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(100)), border: Border.all(width: 1, color: Color(0xFF1B1C28))),
  140. width: 166,
  141. height: 166,
  142. margin: EdgeInsets.only(bottom: 84),
  143. child: CircleAvatar(
  144. backgroundImage: AssetImage('images/centerBtn.png'),
  145. child: ClipOval(
  146. child: Material(
  147. color: Colors.transparent,
  148. child: InkWell(
  149. child: Container(
  150. width: 166,
  151. height: 166,
  152. child: Center(
  153. child: UnconstrainedBox(
  154. child: Image.asset('images/join.png', width: 85),
  155. ),
  156. ),
  157. ),
  158. onTap: () {
  159. getOneRoom();
  160. },
  161. )))),
  162. );
  163. }
  164. Widget centerWidget(BuildContext context) {
  165. double width = MediaQuery.of(context).size.width;
  166. double height = MediaQuery.of(context).size.height;
  167. String endSTring = '';
  168. if (seasonList.isNotEmpty) {
  169. int _time = seasonList[nowIndex].competitionSeason.endTime - DateTime.now().millisecondsSinceEpoch;
  170. _time = _time ~/ 1000 ~/ 3600 ~/ 24;
  171. endSTring = '倒计时 ' + _time.toString() + ' 天';
  172. }
  173. return Column(
  174. children: <Widget>[
  175. Expanded(
  176. child: Stack(
  177. children: <Widget>[
  178. Container(
  179. child: seasonList.isNotEmpty
  180. ? Swiper(
  181. index: nowIndex,
  182. itemCount: seasonList.length,
  183. scrollDirection: Axis.horizontal,
  184. loop: false,
  185. onTap: (index) {
  186. Navigator.push(
  187. context,
  188. CupertinoPageRoute(
  189. builder: (context) =>
  190. RankList(raceId: seasonList[index].competitionSeason.id, gameId: seasonList[index].competitionSeason.gameId)));
  191. },
  192. onIndexChanged: (index) {
  193. setState(() {
  194. nowIndex = index;
  195. });
  196. },
  197. itemBuilder: (context, index) {
  198. return Center(
  199. child: Container(
  200. width: 0.64 * (height - 334),
  201. height: 0.64 * (height - 334),
  202. decoration: BoxDecoration(image: DecorationImage(image: AssetImage('images/home_icon_yuan.png'), fit: BoxFit.contain)),
  203. child: Column(
  204. mainAxisAlignment: MainAxisAlignment.center,
  205. children: <Widget>[
  206. Text('赛季奖金', style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 13)),
  207. Container(
  208. height: 0.1 * (height - 334),
  209. ),
  210. Row(
  211. mainAxisAlignment: MainAxisAlignment.center,
  212. crossAxisAlignment: CrossAxisAlignment.end,
  213. children: _moneyList(),
  214. ),
  215. Container(
  216. height: 0.06 * (height - 334),
  217. ),
  218. // Column(
  219. // children: <Widget>[
  220. // Text('当前排名',
  221. // style: TextStyle(
  222. // color: Color(0xFFFFFFFF),
  223. // fontSize: 13)),
  224. // Text('2',
  225. // style: TextStyle(
  226. // color: Color(0xFFFFFFFF),
  227. // fontSize: 13))
  228. // ],
  229. // )
  230. ],
  231. ),
  232. ));
  233. },
  234. )
  235. : Container()),
  236. Positioned(
  237. left: 10,
  238. top: 0,
  239. width: 48,
  240. height: 48,
  241. child: Material(
  242. color: Colors.transparent,
  243. child: Builder(
  244. builder: (context) => InkWell(
  245. onTap: () {
  246. Scaffold.of(context).openDrawer();
  247. },
  248. child: Padding(
  249. padding: EdgeInsets.all(12),
  250. child: Image.asset(
  251. 'images/person.png',
  252. width: 24,
  253. ),
  254. ),
  255. ),
  256. ),
  257. ),
  258. ),
  259. Positioned(
  260. right: 10,
  261. top: 0,
  262. width: 48,
  263. height: 48,
  264. child: Material(
  265. color: Colors.transparent,
  266. child: InkWell(
  267. onTap: () {
  268. Navigator.push(context, CupertinoPageRoute(builder: (context) => Setting()));
  269. },
  270. child: Padding(
  271. padding: EdgeInsets.all(12),
  272. child: Image.asset('images/setting.png', width: 24),
  273. ),
  274. ),
  275. ),
  276. ),
  277. Positioned(
  278. top: 15,
  279. left: 0,
  280. right: 0,
  281. child: Column(
  282. mainAxisAlignment: MainAxisAlignment.center,
  283. crossAxisAlignment: CrossAxisAlignment.center,
  284. children: <Widget>[
  285. // Text(
  286. // seasonList.length > 0
  287. // ? seasonList[nowIndex].competitionSeason.season
  288. // : '',
  289. // style: TextStyle(color: Colors.white, fontSize: 14),
  290. // ),
  291. Text(
  292. seasonList.isNotEmpty ? seasonList[nowIndex].gameName : '',
  293. style: TextStyle(color: Colors.white, fontSize: 14),
  294. )
  295. ],
  296. ),
  297. ),
  298. Positioned(
  299. bottom: 20,
  300. left: 0,
  301. right: 0,
  302. child: Column(
  303. mainAxisAlignment: MainAxisAlignment.center,
  304. crossAxisAlignment: CrossAxisAlignment.center,
  305. children: <Widget>[
  306. Text(
  307. endSTring,
  308. style: TextStyle(color: Colors.white, fontSize: 14),
  309. )
  310. ],
  311. ),
  312. ),
  313. ],
  314. ),
  315. ),
  316. Builder(
  317. builder: (BuildContext context) {
  318. double aspectRatio = 1;
  319. aspectRatio = width / 334;
  320. return Container(
  321. child: GridView.count(
  322. physics: BouncingScrollPhysics(),
  323. shrinkWrap: true,
  324. crossAxisCount: 2,
  325. childAspectRatio: aspectRatio,
  326. children: <Widget>[
  327. HomeMenu(
  328. 'images/Create.png',
  329. 57,
  330. onTapHomeMenu: () {
  331. Navigator.of(context).push(PageRouteBuilder(
  332. opaque: false,
  333. transitionDuration: Duration(milliseconds: 300),
  334. transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  335. return FadeTransition(
  336. opacity: CurvedAnimation(parent: animation, curve: Curves.linear),
  337. child: child,
  338. );
  339. },
  340. pageBuilder: (BuildContext context, _, __) {
  341. return CreateRoom();
  342. }));
  343. },
  344. ),
  345. HomeMenu(
  346. 'images/search.png',
  347. 31,
  348. onTapHomeMenu: () {
  349. Navigator.push(context, CupertinoPageRoute(builder: (context) => RoomList()));
  350. },
  351. ),
  352. HomeMenu(
  353. 'images/email.png',
  354. 41,
  355. onTapHomeMenu: () async {
  356. bool result = await Navigator.push(context, CupertinoPageRoute(builder: (context) => TipList()));
  357. if (result) {
  358. getUnreadMsg();
  359. }
  360. },
  361. showBadge: showBadge,
  362. ),
  363. HomeMenu(
  364. 'images/shoppingmall.png',
  365. 39,
  366. onTapHomeMenu: () {
  367. Navigator.push(context, CupertinoPageRoute(builder: (context) => Recharge()));
  368. },
  369. ),
  370. ],
  371. ),
  372. );
  373. },
  374. ),
  375. ],
  376. );
  377. }
  378. List<Widget> _moneyList() {
  379. double height = MediaQuery.of(context).size.height;
  380. List<Widget> _list = [];
  381. String bouns = (seasonList[nowIndex].competitionSeason.bonus / 1000).toStringAsFixed(1);
  382. // String bouns = (900000000100 / 1000).toStringAsFixed(1);
  383. // print(bouns);
  384. // print(bouns.indexOf('.0'));
  385. if (bouns.indexOf('.0') != -1 || num.parse(bouns) > 999) {
  386. bouns = bouns.substring(0, bouns.length - 2);
  387. }
  388. List<String> _bounsList = bouns.split('');
  389. List<String> _imageList = [];
  390. for (var item in _bounsList) {
  391. switch (item) {
  392. case '0':
  393. _imageList.add('images/0.png');
  394. break;
  395. case '1':
  396. _imageList.add('images/1.png');
  397. break;
  398. case '2':
  399. _imageList.add('images/2.png');
  400. break;
  401. case '3':
  402. _imageList.add('images/3.png');
  403. break;
  404. case '4':
  405. _imageList.add('images/4.png');
  406. break;
  407. case '5':
  408. _imageList.add('images/5.png');
  409. break;
  410. case '6':
  411. _imageList.add('images/6.png');
  412. break;
  413. case '7':
  414. _imageList.add('images/7.png');
  415. break;
  416. case '8':
  417. _imageList.add('images/8.png');
  418. break;
  419. case '9':
  420. _imageList.add('images/9.png');
  421. break;
  422. case '.':
  423. _imageList.add('.');
  424. break;
  425. }
  426. }
  427. if (_bounsList.isEmpty) {
  428. _imageList.add('images/0.png');
  429. }
  430. for (var item in _imageList) {
  431. if (item != '.') {
  432. if (_imageList.length > 4) {
  433. double _scale = 1 - (_imageList.length - 4) * 0.2;
  434. _list.add(Image.asset(item, height: 0.18 * 0.64 * (height - 334) * _scale));
  435. } else {
  436. _list.add(Image.asset(item, height: 0.18 * 0.64 * (height - 334)));
  437. }
  438. _list.add(Container(width: 5));
  439. } else {
  440. _list.add(Text(
  441. '.',
  442. style: TextStyle(color: Colors.white, fontSize: 36, fontFamily: 'DINEngschrift', height: 0),
  443. ));
  444. }
  445. }
  446. _list.add(Text(
  447. 'K',
  448. style: TextStyle(color: Colors.white, fontSize: 24, fontFamily: 'DINEngschrift', height: .5),
  449. ));
  450. return _list;
  451. }
  452. }
  453. typedef void OnTapHomeMenu();
  454. class HomeMenu extends StatelessWidget {
  455. final String icon;
  456. final double _width;
  457. final OnTapHomeMenu onTapHomeMenu;
  458. final bool showBadge;
  459. HomeMenu(this.icon, this._width, {this.onTapHomeMenu, this.showBadge = false});
  460. @override
  461. Widget build(BuildContext context) {
  462. return Container(
  463. decoration: BoxDecoration(
  464. gradient: LinearGradient(
  465. begin: Alignment.topCenter,
  466. end: Alignment.bottomCenter,
  467. stops: [0.0, 0.01, 0.8],
  468. colors: [Color(0xFF7E89B7), Color(0xFF4E536F), Color(0xFF333558)])),
  469. child: Container(
  470. decoration: BoxDecoration(
  471. border: Border(
  472. // top: BorderSide(width: 2,color: Color(0xFF7E89B7)),
  473. left: BorderSide(width: 0.5, color: Color(0x80000000)),
  474. right: BorderSide(width: 0.5, color: Color(0x80000000)),
  475. bottom: BorderSide(width: 1, color: Color(0x80000000)))),
  476. child: Material(
  477. color: Colors.transparent,
  478. child: InkWell(
  479. onTap: onTapHomeMenu,
  480. child: Container(
  481. child: Center(
  482. child: Image.asset(
  483. icon,
  484. fit: BoxFit.contain,
  485. width: _width,
  486. ),
  487. ),
  488. ),
  489. ),
  490. ),
  491. ),
  492. );
  493. }
  494. }