Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

579 lines
22 KiB

  1. import 'package:chat/generated/i18n.dart';
  2. import 'package:chat/models/daily_bonus.dart';
  3. import 'package:chat/r.dart';
  4. import 'package:chat/utils/CustomUI.dart';
  5. import 'package:chat/utils/HttpUtil.dart';
  6. import 'package:chat/utils/MessageMgr.dart';
  7. import 'package:flutter/cupertino.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:percent_indicator/linear_percent_indicator.dart';
  10. class DailyBonusPage extends StatefulWidget {
  11. @override
  12. State<StatefulWidget> createState() {
  13. return DailyBouusState();
  14. }
  15. }
  16. class DailyBouusState extends State with SingleTickerProviderStateMixin {
  17. bool isLoadingFish = false;
  18. int score = 0;
  19. List<GoodsBean> goods = [];
  20. DailyBonusBean dailyBonusBean;
  21. int addScore = 0;
  22. double progress1 = 0;
  23. double progress2 = 0;
  24. double progress3 = 0;
  25. AnimationController controller;
  26. Animation<double> animation;
  27. AnimationStatusListener statusListener;
  28. @override
  29. void dispose() {
  30. animation.removeStatusListener(statusListener);
  31. animation = null;
  32. controller.dispose();
  33. controller = null;
  34. super.dispose();
  35. }
  36. @override
  37. void initState() {
  38. super.initState();
  39. dailyBonusBean = new DailyBonusBean();
  40. controller = new AnimationController(
  41. duration: const Duration(milliseconds: 800), vsync: this);
  42. animation = Tween(begin: 0.95, end: 1.1).animate(controller);
  43. animation.addStatusListener(statusListener = (status) {
  44. if (status == AnimationStatus.completed) {
  45. controller.reverse(from: 1.0);
  46. } else if (status == AnimationStatus.dismissed) {
  47. controller.forward();
  48. }
  49. });
  50. controller.addListener(() {
  51. setState(() {});
  52. // print(animation.value);
  53. });
  54. controller.forward();
  55. WidgetsBinding.instance.addPostFrameCallback((_) {
  56. initDailyInfo();
  57. });
  58. }
  59. initDailyInfo() async {
  60. var daily = await HttpUtil().getDailyBonus();
  61. if (daily != null) {
  62. dailyBonusBean = daily;
  63. setState(() {
  64. goods = dailyBonusBean.goods;
  65. print('dailyBonusBean goods length:${dailyBonusBean.goods.length}');
  66. score = dailyBonusBean.score;
  67. int currentDay = dailyBonusBean.isSignIn == 0
  68. ? dailyBonusBean.currentDay + 1
  69. : dailyBonusBean.currentDay;
  70. addScore = currentDay * dailyBonusBean.addScore;
  71. setProgress();
  72. isLoadingFish = true;
  73. });
  74. }
  75. }
  76. setProgress() {
  77. int currentPart = 0;
  78. double tempProgress = 0;
  79. for (int k = 0; k < dailyBonusBean.goods.length; k++) {
  80. GoodsBean bean = dailyBonusBean.goods[k];
  81. if (score <= bean.s) {
  82. currentPart = k;
  83. break;
  84. }
  85. }
  86. print('tempProgress: $tempProgress');
  87. print('currentPart: $currentPart');
  88. if (currentPart == 0) {
  89. GoodsBean bean = dailyBonusBean.goods[0];
  90. print('GGG $score ${bean.s} ${dailyBonusBean.goods.length}');
  91. tempProgress = score / bean.s;
  92. progress1 = tempProgress;
  93. progress2 = 0;
  94. progress3 = 0;
  95. if (progress1 > 1) {
  96. progress1 = 1;
  97. }
  98. } else if (currentPart == 1) {
  99. int s = dailyBonusBean.goods[currentPart].s -
  100. dailyBonusBean.goods[currentPart - 1].s;
  101. int current = (score - dailyBonusBean.goods[currentPart - 1].s);
  102. double p = current / s;
  103. progress1 = 1;
  104. progress2 = p;
  105. print('WWW $s $current $progress2 ');
  106. progress3 = 0;
  107. if (progress2 > 1) {
  108. progress2 = 1;
  109. }
  110. } else if (currentPart == 2) {
  111. int s = dailyBonusBean.goods[currentPart].s -
  112. dailyBonusBean.goods[currentPart - 1].s;
  113. int current = (score - dailyBonusBean.goods[currentPart - 1].s);
  114. double p = current / s;
  115. progress1 = 1;
  116. progress2 = 1;
  117. progress3 = p;
  118. if (progress3 > 1) {
  119. progress3 = 1;
  120. }
  121. print('WWW $s $progress3 ');
  122. }
  123. // print('progress: $progress');
  124. // GoodsBean bean = dailyBonusBean.goods[currentPart];
  125. // print('currentPart:$currentPart');
  126. // int max = bean.s;
  127. // double maxProgress = currentPart/(dailyBonusBean.goods.length);
  128. //
  129. // progress = score/max*maxProgress;
  130. }
  131. @override
  132. Widget build(BuildContext context) {
  133. List<String> signStr = I18n.of(context).today_score.split('/s1');
  134. int signTotalDay = dailyBonusBean != null ? dailyBonusBean.currentDay : 0;
  135. String rules = '''${I18n.of(context).sign_tips}''';
  136. Widget content = Material(
  137. child: Stack(
  138. children: <Widget>[
  139. Container(
  140. height: MediaQuery.of(context).size.height,
  141. color: Color(0xffF0F0F0),
  142. ),
  143. Container(
  144. child: Image.asset(
  145. 'assets/images/daily_bg_1.png',
  146. width: MediaQuery.of(context).size.width,
  147. height: 200,
  148. fit: BoxFit.cover,
  149. ),
  150. ),
  151. Scaffold(
  152. appBar: AppBar(
  153. title: Text(
  154. I18n.of(context).sign_in,
  155. textScaleFactor: 1.0,
  156. style: TextStyle(color: Colors.white),
  157. ),
  158. centerTitle: true,
  159. leading: CustomUI.buildCustomLeading(context),
  160. backgroundColor: Colors.transparent,
  161. iconTheme: IconThemeData(color: Colors.white),
  162. ),
  163. backgroundColor: Colors.transparent,
  164. body: SafeArea(
  165. child: SingleChildScrollView(
  166. child: Column(
  167. children: <Widget>[
  168. Padding(
  169. padding: EdgeInsets.symmetric(horizontal: 10),
  170. child: Stack(
  171. children: <Widget>[
  172. Image.asset(R.assetsImagesDailyBg2, fit: BoxFit.cover),
  173. Container(
  174. padding: EdgeInsets.only(left: 30, right: 20),
  175. height: 150,
  176. alignment: Alignment.center,
  177. child: Row(
  178. children: <Widget>[
  179. Expanded(
  180. flex: 4,
  181. child: Column(
  182. crossAxisAlignment:
  183. CrossAxisAlignment.center,
  184. children: <Widget>[
  185. SizedBox(
  186. height: 50,
  187. ),
  188. Text(
  189. I18n.of(context).my_score,
  190. textScaleFactor: 1.0,
  191. style: TextStyle(
  192. color: Color(0xff6D6D6E),
  193. fontSize: 16),
  194. ),
  195. Text(
  196. '$score',
  197. textScaleFactor: 1.0,
  198. style: TextStyle(
  199. color: Color(0xff0060FF),
  200. fontSize: 33),
  201. ),
  202. ],
  203. )),
  204. Expanded(
  205. flex: 6,
  206. child: Container(
  207. padding: EdgeInsets.only(left: 20),
  208. child: Column(
  209. children: <Widget>[
  210. SizedBox(
  211. height: 50,
  212. ),
  213. GestureDetector(
  214. child: Container(
  215. alignment: Alignment.center,
  216. constraints: BoxConstraints(
  217. minHeight: 38,
  218. minWidth: 113,
  219. maxWidth: 150),
  220. child: Text(
  221. I18n.of(context).signin_now,
  222. textScaleFactor: 1.0,
  223. style: TextStyle(
  224. color: Colors.white,
  225. fontSize: 16),
  226. ),
  227. decoration: BoxDecoration(
  228. color: Color(
  229. dailyBonusBean.isSignIn == 0
  230. ? 0xff2D81FF
  231. : 0xffB5B5B5),
  232. borderRadius:
  233. BorderRadius.circular(6)),
  234. ),
  235. onTap: () {
  236. signInBouus();
  237. },
  238. ),
  239. SizedBox(
  240. height: 10,
  241. ),
  242. RichText(
  243. maxLines: 3,
  244. textAlign: TextAlign.center,
  245. text: TextSpan(children: [
  246. TextSpan(
  247. text: signStr[0],
  248. style: TextStyle(
  249. color: Color(0xff6D6D6E),
  250. fontSize: 10)),
  251. TextSpan(
  252. text: '$addScore',
  253. style: TextStyle(
  254. color: Color(0xffFF0000),
  255. fontSize: 10)),
  256. TextSpan(
  257. text: signStr[1],
  258. style: TextStyle(
  259. color: Color(0xff6D6D6E),
  260. fontSize: 10)),
  261. ]))
  262. ],
  263. ),
  264. ))
  265. ],
  266. ),
  267. )
  268. ],
  269. ),
  270. ),
  271. SizedBox(
  272. height: 15,
  273. ),
  274. Container(
  275. ///签到进度
  276. height: 118,
  277. width: MediaQuery.of(context).size.width,
  278. margin: EdgeInsets.symmetric(horizontal: 17),
  279. child: Column(
  280. children: <Widget>[
  281. Container(
  282. alignment: Alignment.center,
  283. height: 105,
  284. width: 360,
  285. child: Stack(
  286. children: <Widget>[
  287. Container(
  288. alignment: Alignment.center,
  289. child: Padding(
  290. padding: EdgeInsets.only(left: 10, top: 12),
  291. child: Row(
  292. children: <Widget>[
  293. LinearPercentIndicator(
  294. width: 51,
  295. lineHeight: 10,
  296. percent: progress1,
  297. padding: null,
  298. progressColor: Color(0xff008AFF),
  299. backgroundColor: Color(0xffD3D3D3),
  300. ),
  301. SizedBox(
  302. width: 15,
  303. ),
  304. LinearPercentIndicator(
  305. width: 94,
  306. lineHeight: 10,
  307. percent: progress2,
  308. padding: null,
  309. progressColor: Color(0xff008AFF),
  310. backgroundColor: Color(0xffD3D3D3),
  311. ),
  312. SizedBox(
  313. width: 23,
  314. ),
  315. LinearPercentIndicator(
  316. width: 94,
  317. lineHeight: 10,
  318. percent: progress3,
  319. padding: null,
  320. progressColor: Color(0xff008AFF),
  321. backgroundColor: Color(0xffD3D3D3),
  322. ),
  323. ],
  324. ),
  325. ),
  326. ),
  327. getGoods()
  328. ],
  329. ),
  330. ),
  331. ],
  332. ),
  333. decoration: BoxDecoration(
  334. color: Colors.white,
  335. borderRadius: BorderRadius.circular(6)),
  336. ),
  337. SizedBox(
  338. height: 22,
  339. ),
  340. Container(
  341. ///签到天数
  342. constraints: BoxConstraints(minHeight: 420),
  343. width: MediaQuery.of(context).size.width,
  344. margin: EdgeInsets.symmetric(horizontal: 17),
  345. child: Column(
  346. children: <Widget>[
  347. Container(
  348. alignment: Alignment.center,
  349. height: 46,
  350. margin: EdgeInsets.all(20),
  351. child: Row(
  352. children: <Widget>[
  353. Image.asset(R.assetsImagesDailyIcon),
  354. Expanded(
  355. child: Text(
  356. I18n.of(context).sign_in_day.replaceFirst(
  357. '/s1', signTotalDay.toString()),
  358. textAlign: TextAlign.center,
  359. textScaleFactor: 1.0,
  360. style: TextStyle(
  361. color: Colors.white, fontSize: 13),
  362. ))
  363. ],
  364. ),
  365. decoration: BoxDecoration(
  366. borderRadius: BorderRadius.circular(23),
  367. gradient: LinearGradient(
  368. colors: [Color(0xff8683EA), Color(0xff4BAAFB)],
  369. )),
  370. ),
  371. ///签到天数表格
  372. Container(
  373. height: 246,
  374. padding: EdgeInsets.only(left: 20, right: 20),
  375. child: GridView.builder(
  376. physics: NeverScrollableScrollPhysics(),
  377. gridDelegate:
  378. SliverGridDelegateWithFixedCrossAxisCount(
  379. crossAxisCount: 7,
  380. crossAxisSpacing: 15.0,
  381. mainAxisSpacing: 13.0),
  382. itemBuilder: (context, index) {
  383. bool isCheck = index < dailyBonusBean.currentDay;
  384. return ClipOval(
  385. child: Container(
  386. alignment: Alignment.center,
  387. color:
  388. Color(isCheck ? 0xff2D81FF : 0xffD3D3D3),
  389. child: isCheck
  390. ? Icon(
  391. Icons.check,
  392. color: Colors.white,
  393. )
  394. : Text(
  395. '${index + 1}',
  396. textScaleFactor: 1.0,
  397. style: TextStyle(color: Colors.white),
  398. ),
  399. ),
  400. );
  401. },
  402. itemCount: dailyBonusBean.days,
  403. padding: EdgeInsets.all(5.0),
  404. ),
  405. ),
  406. Padding(
  407. padding: EdgeInsets.fromLTRB(18, 0, 18, 18),
  408. child: Text(
  409. rules,
  410. textScaleFactor: 1.0,
  411. style: TextStyle(
  412. color: Color(0xffFF0000), fontSize: 12),
  413. ),
  414. )
  415. ],
  416. ),
  417. decoration: BoxDecoration(
  418. color: Colors.white,
  419. borderRadius: BorderRadius.circular(6)),
  420. ),
  421. SizedBox(
  422. height: 22,
  423. ),
  424. ],
  425. ),
  426. )),
  427. )
  428. ],
  429. ),
  430. );
  431. return CustomUI.buildPageLoading(context, content, !isLoadingFish);
  432. }
  433. Widget getGoods() {
  434. return goods.length < 3
  435. ? Container()
  436. : Container(
  437. width: 400,
  438. height: 105,
  439. child: Row(
  440. children: <Widget>[
  441. SizedBox(
  442. width: 46,
  443. ),
  444. getGoodsItem(goods[0], 0),
  445. SizedBox(
  446. width: 39,
  447. ),
  448. getGoodsItem(goods[1], 1),
  449. SizedBox(
  450. width: 38,
  451. ),
  452. getGoodsItem(goods[2], 2),
  453. ],
  454. ),
  455. );
  456. }
  457. getGoodsItem(GoodsBean bean, int index) {
  458. bool isEnough = score >= bean.s;
  459. bool needAnimate = isEnough && bean.state == 0;
  460. return GestureDetector(
  461. onTap: () {
  462. exchangeBonus(index);
  463. },
  464. child: Column(
  465. children: <Widget>[
  466. Container(
  467. alignment: Alignment.center,
  468. margin: EdgeInsets.only(top: 12, bottom: 5),
  469. constraints: BoxConstraints(minWidth: 61),
  470. padding: EdgeInsets.fromLTRB(3, 1, 3, 1),
  471. decoration: BoxDecoration(
  472. color: Color(isEnough ? 0xff2D81FF : 0xffCBCBCB),
  473. borderRadius: BorderRadius.circular(8)),
  474. child: Text(
  475. bean.state == 1
  476. ? I18n.of(context).have_get
  477. : '${bean.h}',
  478. textScaleFactor: 1.0,
  479. style: TextStyle(color: Colors.white, fontSize: 12),
  480. ),
  481. ),
  482. Stack(
  483. children: <Widget>[
  484. // Container(width: 49,),
  485. Positioned(
  486. top: 48,
  487. child: Text('${bean.s}',
  488. textScaleFactor: 1.0,
  489. style: TextStyle(
  490. color: Color(isEnough ? 0xff2D81FF : 0xffCBCBCB),
  491. fontSize: 13))),
  492. Container(
  493. margin: EdgeInsets.only(bottom: 25),
  494. alignment: Alignment.center,
  495. width: 40,
  496. height: 40,
  497. child: Container(
  498. alignment: Alignment.center,
  499. width: 37,
  500. height: 37,
  501. child: Image.asset(
  502. R.assetsImagesImgFyj,
  503. width: needAnimate ? animation.value * 24 : 24,
  504. height: needAnimate ? animation.value * 24 : 24,
  505. ),
  506. decoration: BoxDecoration(
  507. color: Color(isEnough ? 0xff2D81FF : 0xffCBCBCB),
  508. borderRadius: BorderRadius.circular(30))),
  509. decoration: BoxDecoration(
  510. color: Colors.white,
  511. borderRadius: BorderRadius.circular(30)),
  512. ),
  513. ],
  514. ),
  515. ],
  516. ),
  517. );
  518. }
  519. signInBouus() async {
  520. if (dailyBonusBean.isSignIn == 0) {
  521. int addScore = (dailyBonusBean.currentDay + 1) * dailyBonusBean.addScore;
  522. bool isSuccess = await HttpUtil().signInBounus(addScore);
  523. if (isSuccess) {
  524. setState(() {
  525. score = score + addScore;
  526. dailyBonusBean.isSignIn = 1;
  527. dailyBonusBean.currentDay = dailyBonusBean.currentDay + 1;
  528. });
  529. }
  530. }
  531. }
  532. exchangeBonus(int index) async {
  533. GoodsBean goodsBean = goods[index];
  534. if (score >= goodsBean.s && goodsBean.state == 0) {
  535. // int addScore= (dailyBonusBean.currentDay+1)*dailyBonusBean.addScore;
  536. bool isSuccess = await HttpUtil().signInExchange(goodsBean);
  537. if (isSuccess) {
  538. setState(() {
  539. score = score - goodsBean.s;
  540. goods[index].state = 1;
  541. setProgress();
  542. });
  543. MessageMgr().emit('refresh_money');
  544. ///兑换成功后加了金币刷新余额
  545. }
  546. }
  547. }
  548. }