root_page.dart 18 KB

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