root_page.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:bbyyy/beans/coupon_bean_entity.dart';
  4. import 'package:bbyyy/beans/grab_a_red_envelope_bean_entity.dart';
  5. import 'package:bbyyy/beans/user_balance_entity.dart';
  6. import 'package:bbyyy/beans/user_bean_entity.dart';
  7. import 'package:bbyyy/https/MyDio.dart';
  8. import 'package:bbyyy/https/my_request.dart';
  9. import 'package:bbyyy/my_tools/daily_red_packet/daily_red_packet.dart';
  10. import 'package:bbyyy/my_tools/dims.dart';
  11. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  12. import 'package:bbyyy/my_tools/event_bus.dart';
  13. import 'package:bbyyy/my_tools/loacion_util.dart';
  14. import 'package:bbyyy/my_tools/my_colors.dart';
  15. import 'package:bbyyy/my_tools/my_cookie.dart';
  16. import 'package:bbyyy/my_tools/my_datas.dart';
  17. import 'package:bbyyy/my_tools/my_tools.dart';
  18. import 'package:bbyyy/my_tools/pop_up_queue.dart';
  19. import 'package:bbyyy/my_tools/s.dart';
  20. import 'package:bbyyy/nsq/nsq.dart' as Nsq;
  21. import 'package:bbyyy/paegs/gang_page/gang_page.dart';
  22. import 'package:bbyyy/paegs/home_page/home_page.dart';
  23. import 'package:bbyyy/paegs/mine_page/bind_alipay_page/bind_alipay_page.dart';
  24. import 'package:bbyyy/paegs/mine_page/mine_page.dart';
  25. import 'package:bbyyy/paegs/mine_page/order_page/order_page.dart';
  26. import 'package:bbyyy/paegs/mine_page/wallet_page/withdraw_page/withdraw_page.dart';
  27. import 'package:bbyyy/paegs/msg_page/msg_page.dart';
  28. import 'package:bbyyy/paegs/root_page/root_page_view.dart';
  29. import 'package:bbyyy/pay/pay_tools.dart';
  30. import 'package:flutter/cupertino.dart';
  31. import 'package:flutter/gestures.dart';
  32. import 'package:flutter/material.dart';
  33. import 'package:flutter/services.dart';
  34. import 'package:flutter_svg/flutter_svg.dart';
  35. class RootPage extends StatefulWidget {
  36. @override
  37. _RootPageState createState() => _RootPageState();
  38. }
  39. class _RootPageState extends State<RootPage> {
  40. PageController pageController = PageController(initialPage: 0);
  41. DateTime lastPopTime;
  42. double ppx;
  43. @override
  44. void initState() {
  45. super.initState();
  46. // s().encryption();
  47. LocationUtil().initLocation(context);
  48. LocationUtil().getLocation();
  49. Nsq.ConnectServer(
  50. Nsq.handlerMessage, // 接收消息处理逻辑
  51. // serverIp: '172.16.104.14',
  52. serverIp: MyCookie().server,
  53. // 服务端返回
  54. serverPort: 4150,
  55. // 服务端返回
  56. topic: '${MyCookie().getUID()}',
  57. // 主题订阅,服务端返回
  58. channel: '${MyCookie().getUID()}',
  59. // 服务端返回uid
  60. clientId: '客户uid',
  61. // 服务端返回uid
  62. hostname: '主机名', // 服务端返回
  63. );
  64. EventBus().emit('检查粘贴板');
  65. EventBus().on('ChangePage', (arg) {
  66. if (RootPageView().bNIndex == 3) {
  67. queryPersonalInformation();
  68. }
  69. pageController.jumpToPage(RootPageView().bNIndex);
  70. setState(() {});
  71. });
  72. EventBus().on('hasNoPay', (arg) {
  73. setState(() {});
  74. });
  75. EventBus().on('showRP', (arg) {
  76. signInRedEnvelope();
  77. });
  78. MyData().init(context);
  79. queryPersonalInformation();
  80. checkForUpdates(context);
  81. Future.delayed(Duration(seconds: 1), () {
  82. // checkCouponEvent();
  83. // DailyRedPacketTool().queryRedEnvelopeInformation();
  84. getUserWalletBalance((re, hE) {
  85. if (!hE) {
  86. UserBalanceEntity balance =
  87. UserBalanceEntity.fromJson(json.decode(re.data.toString()));
  88. MyCookie().balance = balance.data;
  89. }
  90. }, (e) {}, context);
  91. });
  92. }
  93. @override
  94. void dispose() {
  95. // TODO: implement dispose
  96. EventBus().off('ChangePage');
  97. EventBus().off('hasNoPay');
  98. EventBus().off('showRP');
  99. LocationUtil().stopLocation();
  100. super.dispose();
  101. }
  102. @override
  103. Widget build(BuildContext context) {
  104. ppx = MediaQuery.of(context).size.width / 375;
  105. return WillPopScope(
  106. onWillPop: () async {
  107. if (lastPopTime == null ||
  108. DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
  109. lastPopTime = DateTime.now();
  110. EasyLoading.showToast('再按一次退出');
  111. } else {
  112. lastPopTime = DateTime.now();
  113. await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  114. }
  115. return false;
  116. },
  117. child: Scaffold(
  118. resizeToAvoidBottomInset: false,
  119. backgroundColor: Colors.transparent,
  120. body: Stack(
  121. children: [
  122. Column(
  123. children: [
  124. Expanded(
  125. child: PageView(
  126. controller: pageController,
  127. children: [HomePage(), GangPage(), MsgPage(), MinePage()],
  128. physics: NeverScrollableScrollPhysics(),
  129. ),
  130. ),
  131. RootPageView().bottomNavigationBar(),
  132. ],
  133. ),
  134. if (MyCookie().haveNoPay)
  135. Positioned(
  136. bottom: 60,
  137. right: 0,
  138. child: SafeArea(
  139. top: false,
  140. child: GestureDetector(
  141. onTap: () {
  142. MyTools().toPage(
  143. context,
  144. OrderPage(
  145. index: 2,
  146. ), (then) {
  147. queryPersonalInformation();
  148. });
  149. },
  150. behavior: HitTestBehavior.translucent,
  151. child: Container(
  152. height: 44,
  153. width: 100,
  154. padding: EdgeInsets.symmetric(horizontal: 10),
  155. child: Row(
  156. children: [
  157. SvgPicture.asset(
  158. 'images/svg/订单.svg',
  159. color: Colors.white,
  160. height: 15,
  161. width: 15,
  162. ),
  163. Text(
  164. '未付订单',
  165. style: TextStyle(color: Colors.white, fontSize: 14),
  166. )
  167. ],
  168. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  169. ),
  170. decoration: BoxDecoration(
  171. color: Colors.amber,
  172. borderRadius: BorderRadius.only(
  173. topLeft: Radius.circular(22),
  174. bottomLeft: Radius.circular(22),
  175. ),
  176. ),
  177. ),
  178. ),
  179. ),
  180. ),
  181. ],
  182. ),
  183. ),
  184. );
  185. }
  186. void queryPersonalInformation() {
  187. MyDio().query({
  188. "key": "user",
  189. "filters": {
  190. "conditions": ["uid == ${MyCookie().getUID()}"]
  191. },
  192. "dims": userDims,
  193. "paging": [1, 20]
  194. }, (response, hasError) {
  195. if (!hasError) {
  196. UserBeanEntity entity =
  197. UserBeanEntity.fromJson(json.decode(response.data.toString()));
  198. MyCookie().userBean = entity.data.data[0];
  199. EventBus().emit('userChange');
  200. }
  201. }, (error) {});
  202. }
  203. //查询优惠券活动
  204. // checkCouponEvent() {
  205. // MyDio().query({
  206. // "key": "coupon_distribute",
  207. // "filters": {
  208. // "conditions": ["valid==true"],
  209. // },
  210. // "dims": couponDistributeDims,
  211. // "paging": [1, 100]
  212. // }, (response, hasError) {
  213. // if (!hasError) {
  214. // CouponBeanEntity entity =
  215. // CouponBeanEntity.fromJson(json.decode(response.data.toString()));
  216. // PopUpQueue().coupon.addAll(entity.data.data);
  217. // }
  218. // }, (error) {});
  219. // }
  220. int showWitch = 0;
  221. //登录红包
  222. signInRedEnvelope() {
  223. showDialog<void>(
  224. context: context,
  225. barrierDismissible: false,
  226. // false = user must tap button, true = tap outside dialog
  227. builder: (BuildContext dialogContext) {
  228. return StatefulBuilder(
  229. builder:
  230. (BuildContext context, void Function(void Function()) setState) {
  231. return Container(
  232. child: Column(
  233. children: [
  234. showRedEnvelope(setState),
  235. GestureDetector(
  236. onTap: () {
  237. showWitch = 0;
  238. Navigator.pop(context);
  239. },
  240. child: Container(
  241. margin: EdgeInsets.only(top: 24 * ppx),
  242. child: Icon(
  243. Icons.clear,
  244. color: Colors.white,
  245. ),
  246. height: 28 * ppx,
  247. width: 28 * ppx,
  248. alignment: Alignment.center,
  249. decoration: BoxDecoration(
  250. color: MyColors.c25E7E7E7,
  251. borderRadius: BorderRadius.circular(14 * ppx)),
  252. ),
  253. )
  254. ],
  255. mainAxisAlignment: MainAxisAlignment.center,
  256. ),
  257. alignment: Alignment.center,
  258. );
  259. },
  260. );
  261. },
  262. );
  263. }
  264. Stack showRedEnvelope(void Function(void Function() p1) setState) {
  265. switch (showWitch) {
  266. case 0:
  267. return redEnvelopeP1(setState);
  268. case 1:
  269. return redEnvelopeP2(setState);
  270. case 2:
  271. return redEnvelopeP3(setState);
  272. default:
  273. return redEnvelopeP1(setState);
  274. }
  275. }
  276. bool grab = false;
  277. Stack redEnvelopeP1(void Function(void Function() p1) setState) {
  278. return Stack(
  279. children: [
  280. Positioned(
  281. child: Container(
  282. height: 396 * ppx,
  283. width: double.infinity,
  284. decoration: BoxDecoration(
  285. borderRadius: BorderRadius.circular(16),
  286. color: MyColors.cF25D4B),
  287. margin: EdgeInsets.symmetric(horizontal: 45 * ppx),
  288. alignment: Alignment.bottomCenter,
  289. child: CustomPaint(
  290. painter: RedEnvelopePath(),
  291. size: Size(double.infinity - (90 * ppx), 120 * ppx)),
  292. ),
  293. ),
  294. Positioned(
  295. bottom: 40 * ppx,
  296. left: ((375 / 2 - 45) * ppx),
  297. child: GestureDetector(
  298. onTap: () {
  299. setState(() {
  300. grab = true;
  301. });
  302. if (DailyRedPacketTool().dailyRedPacket.valid) {
  303. EventBus().on('grabARedEnvelope', (arg) {
  304. if (arg is GrabARedEnvelopeBeanData) {
  305. if (arg.success) {
  306. setState(() {
  307. showWitch = 1;
  308. });
  309. } else {
  310. setState(() {
  311. showWitch = 2;
  312. });
  313. }
  314. }
  315. EventBus().off('grabARedEnvelope');
  316. });
  317. DailyRedPacketTool().grabARedEnvelope();
  318. } else {
  319. setState(() {
  320. showWitch = 2;
  321. });
  322. }
  323. },
  324. behavior: HitTestBehavior.translucent,
  325. child: Container(
  326. decoration: BoxDecoration(
  327. color: MyColors.cF1D19B,
  328. borderRadius: BorderRadius.circular(45 * ppx)),
  329. height: 90 * ppx,
  330. width: 90 * ppx,
  331. child: grab
  332. ? Container(
  333. height: 40,
  334. width: 40,
  335. padding: const EdgeInsets.all(2.0),
  336. child: CircularProgressIndicator(
  337. backgroundColor: Colors.transparent,
  338. valueColor: AlwaysStoppedAnimation(Colors.white),
  339. strokeWidth: 2,
  340. ),
  341. )
  342. : Text(
  343. '抢',
  344. style: TextStyle(
  345. color: MyColors.c333333,
  346. fontSize: 40 * ppx,
  347. decoration: TextDecoration.none,
  348. fontWeight: FontWeight.normal),
  349. ),
  350. alignment: Alignment.center,
  351. ),
  352. ),
  353. ),
  354. Positioned(
  355. child: Column(
  356. children: [
  357. SvgPicture.asset('images/svg/logo.svg'),
  358. Container(
  359. margin: EdgeInsets.only(top: 28 * ppx),
  360. child: Text(
  361. '登录红包',
  362. style: TextStyle(
  363. color: Colors.white,
  364. fontSize: 32 * ppx,
  365. fontWeight: FontWeight.normal,
  366. decoration: TextDecoration.none),
  367. ),
  368. ),
  369. Container(
  370. margin: EdgeInsets.only(top: 4 * ppx),
  371. child: Text(
  372. '每人仅限1份',
  373. style: TextStyle(
  374. color: MyColors.cF1D19B,
  375. fontSize: 14 * ppx,
  376. fontWeight: FontWeight.normal,
  377. decoration: TextDecoration.none),
  378. ),
  379. )
  380. ],
  381. ),
  382. top: 37 * ppx,
  383. )
  384. ],
  385. alignment: Alignment.topCenter,
  386. );
  387. }
  388. Stack redEnvelopeP2(void Function(void Function() p1) setState) {
  389. return Stack(
  390. children: [
  391. Positioned(
  392. child: Container(
  393. height: 396 * ppx,
  394. width: double.infinity,
  395. decoration: BoxDecoration(
  396. borderRadius: BorderRadius.circular(16),
  397. color: MyColors.cEB4935),
  398. margin: EdgeInsets.symmetric(horizontal: 45 * ppx),
  399. alignment: Alignment.bottomCenter,
  400. ),
  401. ),
  402. Positioned(
  403. child: Column(
  404. children: [
  405. SvgPicture.asset('images/svg/logo.svg'),
  406. Container(
  407. margin: EdgeInsets.only(top: 67 * ppx),
  408. child: Text(
  409. '${DailyRedPacketTool().grabARedEnvelopeBeanData.amount}元',
  410. style: TextStyle(
  411. color: MyColors.cF1D19B,
  412. fontSize: 38 * ppx,
  413. fontWeight: FontWeight.bold,
  414. decoration: TextDecoration.none),
  415. ),
  416. ),
  417. ],
  418. ),
  419. top: 37 * ppx,
  420. ),
  421. Positioned(
  422. child: RichText(
  423. text: TextSpan(
  424. text: '自动划入钱包,去',
  425. style: TextStyle(
  426. color: Colors.white,
  427. decoration: TextDecoration.none,
  428. fontSize: 13 * ppx),
  429. children: [
  430. TextSpan(
  431. text: '提现',
  432. style: TextStyle(
  433. color: Colors.white,
  434. decoration: TextDecoration.underline,
  435. fontSize: 13 * ppx),
  436. recognizer: TapGestureRecognizer()
  437. ..onTap = () {
  438. if (MyCookie().userBean.aliPayAccount.isEmpty ||
  439. MyCookie().userBean.aliPayName.isEmpty) {
  440. MyTools().toPage(context, BindAlipayPage(),
  441. (then) {
  442. if (MyCookie()
  443. .userBean
  444. .aliPayAccount
  445. .isNotEmpty &&
  446. MyCookie()
  447. .userBean
  448. .aliPayName
  449. .isNotEmpty) {
  450. MyTools().toPage(context, WithdrawPage(),
  451. (then) {
  452. queryPersonalInformation();
  453. });
  454. }
  455. });
  456. } else {
  457. MyTools().toPage(context, WithdrawPage(),
  458. (then) {
  459. queryPersonalInformation();
  460. });
  461. }
  462. }),
  463. ]),
  464. ),
  465. bottom: 20 * ppx,
  466. )
  467. ],
  468. alignment: Alignment.topCenter,
  469. );
  470. }
  471. Stack redEnvelopeP3(void Function(void Function() p1) setState) {
  472. return Stack(
  473. children: [
  474. Positioned(
  475. child: Container(
  476. height: 396 * ppx,
  477. width: double.infinity,
  478. decoration: BoxDecoration(
  479. borderRadius: BorderRadius.circular(16),
  480. color: MyColors.cEB4935),
  481. margin: EdgeInsets.symmetric(horizontal: 45 * ppx),
  482. ),
  483. ),
  484. Positioned(
  485. child: Column(
  486. children: [
  487. SvgPicture.asset('images/svg/logo.svg'),
  488. Container(
  489. margin: EdgeInsets.only(top: 64 * ppx),
  490. padding: EdgeInsets.symmetric(horizontal: 12*ppx),
  491. width: ppx*285,
  492. child: Text(
  493. '${DailyRedPacketTool().grabARedEnvelopeBeanData.msg}',
  494. textAlign: TextAlign.center,
  495. style: TextStyle(
  496. color: MyColors.cF1D19B,
  497. fontSize: 18 * ppx,
  498. fontWeight: FontWeight.normal,
  499. decoration: TextDecoration.none,),
  500. maxLines: 5,
  501. ),
  502. )
  503. ],
  504. ),
  505. top: 37 * ppx,
  506. ),
  507. Positioned(
  508. child: Text(
  509. '谢谢支持,欢迎参与后续活动!',
  510. style: TextStyle(
  511. color: Colors.white,
  512. fontSize: 13 * ppx,
  513. fontWeight: FontWeight.normal,
  514. decoration: TextDecoration.none),
  515. ),
  516. bottom: 20 * ppx,
  517. )
  518. ],
  519. alignment: Alignment.topCenter,
  520. );
  521. }
  522. }
  523. class RedEnvelopePath extends CustomPainter {
  524. Paint _paint = Paint()
  525. ..color = MyColors.cF55544 //画笔颜色
  526. ..strokeCap = StrokeCap.round //画笔笔触类型
  527. ..isAntiAlias = true //是否启动抗锯齿
  528. ..strokeWidth = 15.0; //画笔的宽度
  529. @override
  530. void paint(Canvas canvas, Size size) {
  531. var path = Path()
  532. ..moveTo(0, 0)
  533. ..quadraticBezierTo(size.width / 2, 80, size.width, 0)
  534. ..lineTo(size.width, size.height - 16)
  535. ..quadraticBezierTo(size.width, size.height, size.width - 16, size.height)
  536. ..lineTo(16, size.height)
  537. ..quadraticBezierTo(0, size.height, 0, size.height - 16)
  538. ..close();
  539. canvas.drawPath(path, _paint);
  540. }
  541. @override
  542. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  543. return false;
  544. }
  545. }