root_page.dart 18 KB

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