index.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'dart:convert';
  4. import 'package:app_settings/app_settings.dart';
  5. import 'package:fast/constants.dart';
  6. import 'package:fast/utils/api.dart';
  7. import 'package:fast/utils/global.dart';
  8. import 'package:fast/utils/http_utils.dart';
  9. import 'package:fast/utils/size_fit.dart';
  10. import 'package:fast/view/component/login.dart';
  11. import 'package:fast/view/component/noti_enter.dart';
  12. import 'package:fast/view/home.dart';
  13. import 'package:fast/view/me.dart';
  14. import 'package:flutter/foundation.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:flutter/services.dart';
  17. import 'package:jpush_flutter/jpush_flutter.dart';
  18. // import 'package:app_settings/app_settings.dart';
  19. class IndexScreen extends StatefulWidget {
  20. const IndexScreen({Key? key}) : super(key: key);
  21. @override
  22. State<IndexScreen> createState() => _IndexScreenState();
  23. }
  24. class _IndexScreenState extends State<IndexScreen>
  25. with SingleTickerProviderStateMixin, WidgetsBindingObserver {
  26. late AnimationController controller;
  27. late Animation animation0;
  28. late Animation animation1;
  29. double topOffset = -40;
  30. double bottomOffset = 0;
  31. double bottom = 0;
  32. var welcome = '早上好';
  33. String topCover = 'assets/images/morning.png';
  34. var isNight = false;
  35. var date = '';
  36. int tabIndex = 0;
  37. bool showLogin = false;
  38. String debugLable = 'Unknown'; /*错误信息*/
  39. final JPush jpush = JPush(); /* 初始化极光插件*/
  40. Map<String, dynamic>? onlineObj;
  41. logout() {
  42. setState(() {
  43. showLogin = true;
  44. });
  45. }
  46. login() {
  47. setState(() {
  48. showLogin = false;
  49. });
  50. }
  51. showIndex(){
  52. setState(() {
  53. tabIndex = 0;
  54. });
  55. }
  56. showMe() {
  57. setState(() {
  58. tabIndex = 1;
  59. });
  60. }
  61. @override
  62. void didChangeAppLifecycleState(AppLifecycleState state) {
  63. // TODO: implement didChangeAppLifecycleState
  64. super.didChangeAppLifecycleState(state);
  65. setPush();
  66. }
  67. Future checkEmulator() async{
  68. // bool isAnEmulator = await FlutterIs
  69. }
  70. @override
  71. void initState() {
  72. super.initState();
  73. WidgetsBinding.instance?.addObserver(this);
  74. setPush();
  75. checkEmulator();
  76. Global().mainPage = this;
  77. controller = AnimationController(
  78. vsync: this, duration: const Duration(milliseconds: 2000));
  79. controller.addListener(() {
  80. setState(() {});
  81. });
  82. animation0 = Tween(begin: 0.0, end: 1.0).animate(
  83. CurvedAnimation(parent: controller, curve: const Interval(0.05, 0.2)));
  84. animation1 = Tween(begin: 0.0, end: 1.0).animate(
  85. CurvedAnimation(parent: controller, curve: const Interval(0.825, 1.0)));
  86. Timer(const Duration(milliseconds: 200), () {
  87. controller.forward();
  88. setState(() {
  89. topOffset = 0;
  90. bottomOffset = bottom;
  91. });
  92. });
  93. }
  94. Future getTokenId() async {
  95. String rid = await jpush.getRegistrationID();
  96. String type = 'OTHER';
  97. if (Platform.isIOS) {
  98. type = 'IOS';
  99. } else if (Platform.isAndroid) {
  100. type = 'ANDROID';
  101. }
  102. String data = await HttpUtils.post(Api.bindPushId,
  103. data: {"client_id": rid, "provider": 'JIGUANG', "client_type": type});
  104. print(data);
  105. // Widget okButton = FlatButton(onPressed: (){
  106. // Navigator.of(context).pop();
  107. // }, child: const Text("复制后关闭"));
  108. // AlertDialog alert = AlertDialog(
  109. // title: const Text('极光rid:'),
  110. // content: Text(rid),
  111. // actions: [okButton],
  112. // );
  113. // showDialog(context: context, builder: (BuildContext context){
  114. // return alert;
  115. // });
  116. }
  117. @override
  118. void dispose() {
  119. controller.dispose();
  120. WidgetsBinding.instance?.removeObserver(this);
  121. super.dispose();
  122. }
  123. void setPush() {
  124. if (kIsWeb){
  125. return;
  126. }
  127. if (Platform.isIOS == false && Platform.isAndroid == false) {
  128. return;
  129. }
  130. else {
  131. }
  132. jpush.setup(
  133. appKey: '7cf918ada725a9e9aecc8a17',
  134. production: false,
  135. debug: true,
  136. channel: '');
  137. jpush.addEventHandler(
  138. onReceiveNotification: (Map<String, dynamic> message) async {
  139. print('leon messages:' + message.toString());
  140. },
  141. onReceiveNotificationAuthorization: (event) async {
  142. if (event['isEnabled'] == false && Platform.isIOS) {
  143. Global().allowNotification = false;
  144. return;
  145. print('9527');
  146. // AppSettings.openNotificationSettings();
  147. }
  148. Global().allowNotification = true;
  149. print(event.toString());
  150. getTokenId();
  151. },
  152. onOpenNotification: (Map<String, dynamic> message) async {
  153. print("flutter onOpenNotification: $message");
  154. print(message.toString());
  155. /// 点击通知栏消息,在此时通常可以做一些页面跳转等
  156. // Map<String, dynamic>? detail;
  157. var detail;
  158. if (Platform.isIOS) {
  159. detail = message['extras'];
  160. }
  161. else if (Platform.isAndroid) {
  162. String extra = message['extras']['cn.jpush.android.EXTRA'];
  163. detail = jsonDecode(extra);
  164. }
  165. if (detail!['to'] != null) {
  166. if (detail['to'] == 'my') {
  167. setState(() {
  168. tabIndex = 1;
  169. });
  170. } else if (detail['to'] == 'profit') {
  171. showDialog(
  172. context: context,
  173. barrierDismissible: false,
  174. useSafeArea: false,
  175. barrierColor: const Color(0xF2000D1F),
  176. builder: (BuildContext context) {
  177. return NotifiEnter(fastId: detail!['fasting_id']);
  178. });
  179. }
  180. }
  181. },
  182. );
  183. jpush.applyPushAuthority(
  184. const NotificationSettingsIOS(sound: true, alert: true, badge: true));
  185. jpush.setBadge(0);
  186. if (Platform.isAndroid) {
  187. Global().allowNotification = true;
  188. getTokenId();
  189. }
  190. }
  191. @override
  192. Widget build(BuildContext context) {
  193. SystemUiOverlayStyle style = const SystemUiOverlayStyle(
  194. statusBarColor: Colors.transparent,
  195. statusBarIconBrightness: Brightness.light);
  196. SystemChrome.setSystemUIOverlayStyle(style);
  197. var size = MediaQuery.of(context).size;
  198. // final Controller c = Get.put(Controller());
  199. SizeFit.initialize(context);
  200. double min = 667.px; // 表盘和进食中尺寸判断,小于667则,用小表盘
  201. Global().circleWidth = 330.px;
  202. Global().paintWidth = 40.px;
  203. Global().progressWidth = 117.px;
  204. Global().scale = 1.0;
  205. if (size.height <= min) {
  206. Global().scale = 0.9;
  207. // Global().circleWidth = 280.px;
  208. // Global().paintWidth = 35.px;
  209. // Global().progressWidth = 99.px;
  210. }
  211. EdgeInsets safePadding = MediaQuery.of(context).padding;
  212. bottom = safePadding.bottom;
  213. Widget me = const MePage();
  214. return Scaffold(
  215. body: Stack(
  216. children: [
  217. if (showLogin == false)
  218. Opacity(
  219. opacity: tabIndex == 0 ? 1 : 0,
  220. child: const HomeScreen(),
  221. ),
  222. if (tabIndex == 1) me,
  223. if (showLogin) const Login(),
  224. if (showLogin == false)
  225. AnimatedPositioned(
  226. left: 0,
  227. bottom: bottomOffset,
  228. right: 0,
  229. duration: const Duration(milliseconds: 300),
  230. child: Opacity(
  231. opacity: animation0.value,
  232. child: Container(
  233. color: Colors.transparent,
  234. margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
  235. child: Container(
  236. width: double.infinity,
  237. height: 52.px,
  238. margin: EdgeInsets.fromLTRB(14.px, 0, 14.px, 14.px),
  239. decoration: const BoxDecoration(
  240. color: Color(0xFF142133),
  241. borderRadius: BorderRadius.all(Radius.circular(26))),
  242. child: Flex(
  243. direction: Axis.horizontal,
  244. children: [
  245. Expanded(
  246. child: GestureDetector(
  247. onTap: () {
  248. setState(() {
  249. tabIndex = 0;
  250. Global().homePage.getDatas();
  251. });
  252. },
  253. child: Center(
  254. child: Image.asset(
  255. tabIndex == 0
  256. ? 'assets/images/tab_home_sel.png'
  257. : 'assets/images/tab_home.png',
  258. width: 64.px,
  259. height: 40.px,
  260. ),
  261. ),
  262. )),
  263. Expanded(
  264. child: GestureDetector(
  265. onTap: () {
  266. setState(() {
  267. tabIndex = 1;
  268. });
  269. },
  270. child: Center(
  271. child: Image.asset(
  272. tabIndex == 1
  273. ? 'assets/images/tab_me_sel.png'
  274. : 'assets/images/tab_me.png',
  275. width: 64.px,
  276. height: 40.px,
  277. ),
  278. ),
  279. )),
  280. ],
  281. ),
  282. ),
  283. ),
  284. ),
  285. )
  286. ],
  287. ),
  288. );
  289. }
  290. }