| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- import 'dart:async';
- import 'dart:io';
- import 'dart:convert';
- import 'package:app_settings/app_settings.dart';
- import 'package:fast/constants.dart';
- import 'package:fast/utils/api.dart';
- import 'package:fast/utils/global.dart';
- import 'package:fast/utils/http_utils.dart';
- import 'package:fast/utils/size_fit.dart';
- import 'package:fast/view/component/login.dart';
- import 'package:fast/view/component/noti_enter.dart';
- import 'package:fast/view/home.dart';
- import 'package:fast/view/me.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:jpush_flutter/jpush_flutter.dart';
- // import 'package:app_settings/app_settings.dart';
- class IndexScreen extends StatefulWidget {
- const IndexScreen({Key? key}) : super(key: key);
- @override
- State<IndexScreen> createState() => _IndexScreenState();
- }
- class _IndexScreenState extends State<IndexScreen>
- with SingleTickerProviderStateMixin, WidgetsBindingObserver {
- late AnimationController controller;
- late Animation animation0;
- late Animation animation1;
- double topOffset = -40;
- double bottomOffset = 0;
- double bottom = 0;
- var welcome = '早上好';
- String topCover = 'assets/images/morning.png';
- var isNight = false;
- var date = '';
- int tabIndex = 0;
- bool showLogin = false;
- String debugLable = 'Unknown'; /*错误信息*/
- final JPush jpush = JPush(); /* 初始化极光插件*/
- Map<String, dynamic>? onlineObj;
- logout() {
- setState(() {
- showLogin = true;
- });
- }
- login() {
- setState(() {
- showLogin = false;
- });
- }
- showIndex(){
- setState(() {
- tabIndex = 0;
- });
- }
- showMe() {
- setState(() {
- tabIndex = 1;
- });
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- // TODO: implement didChangeAppLifecycleState
- super.didChangeAppLifecycleState(state);
- setPush();
- }
- Future checkEmulator() async{
- // bool isAnEmulator = await FlutterIs
- }
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance?.addObserver(this);
- setPush();
- checkEmulator();
- Global().mainPage = this;
- controller = AnimationController(
- vsync: this, duration: const Duration(milliseconds: 2000));
- controller.addListener(() {
- setState(() {});
- });
- animation0 = Tween(begin: 0.0, end: 1.0).animate(
- CurvedAnimation(parent: controller, curve: const Interval(0.05, 0.2)));
- animation1 = Tween(begin: 0.0, end: 1.0).animate(
- CurvedAnimation(parent: controller, curve: const Interval(0.825, 1.0)));
- Timer(const Duration(milliseconds: 200), () {
- controller.forward();
- setState(() {
- topOffset = 0;
- bottomOffset = bottom;
- });
- });
- }
- Future getTokenId() async {
- String rid = await jpush.getRegistrationID();
- String type = 'OTHER';
- if (Platform.isIOS) {
- type = 'IOS';
- } else if (Platform.isAndroid) {
- type = 'ANDROID';
- }
- String data = await HttpUtils.post(Api.bindPushId,
- data: {"client_id": rid, "provider": 'JIGUANG', "client_type": type});
- print(data);
- // Widget okButton = FlatButton(onPressed: (){
- // Navigator.of(context).pop();
- // }, child: const Text("复制后关闭"));
- // AlertDialog alert = AlertDialog(
- // title: const Text('极光rid:'),
- // content: Text(rid),
- // actions: [okButton],
- // );
- // showDialog(context: context, builder: (BuildContext context){
- // return alert;
- // });
- }
- @override
- void dispose() {
- controller.dispose();
- WidgetsBinding.instance?.removeObserver(this);
- super.dispose();
- }
- void setPush() {
- if (kIsWeb){
- return;
- }
- if (Platform.isIOS == false && Platform.isAndroid == false) {
- return;
- }
- else {
- }
- jpush.setup(
- appKey: '7cf918ada725a9e9aecc8a17',
- production: false,
- debug: true,
- channel: '');
- jpush.addEventHandler(
- onReceiveNotification: (Map<String, dynamic> message) async {
- print('leon messages:' + message.toString());
- },
- onReceiveNotificationAuthorization: (event) async {
- if (event['isEnabled'] == false && Platform.isIOS) {
- Global().allowNotification = false;
- return;
- print('9527');
- // AppSettings.openNotificationSettings();
- }
- Global().allowNotification = true;
- print(event.toString());
- getTokenId();
- },
- onOpenNotification: (Map<String, dynamic> message) async {
- print("flutter onOpenNotification: $message");
- print(message.toString());
- /// 点击通知栏消息,在此时通常可以做一些页面跳转等
- // Map<String, dynamic>? detail;
- var detail;
- if (Platform.isIOS) {
- detail = message['extras'];
- }
- else if (Platform.isAndroid) {
- String extra = message['extras']['cn.jpush.android.EXTRA'];
- detail = jsonDecode(extra);
- }
- if (detail!['to'] != null) {
- if (detail['to'] == 'my') {
- setState(() {
- tabIndex = 1;
- });
- } else if (detail['to'] == 'profit') {
- showDialog(
- context: context,
- barrierDismissible: false,
- useSafeArea: false,
- barrierColor: const Color(0xF2000D1F),
- builder: (BuildContext context) {
- return NotifiEnter(fastId: detail!['fasting_id']);
- });
- }
- }
- },
- );
- jpush.applyPushAuthority(
- const NotificationSettingsIOS(sound: true, alert: true, badge: true));
- jpush.setBadge(0);
- if (Platform.isAndroid) {
- Global().allowNotification = true;
- getTokenId();
- }
- }
- @override
- Widget build(BuildContext context) {
- SystemUiOverlayStyle style = const SystemUiOverlayStyle(
- statusBarColor: Colors.transparent,
- statusBarIconBrightness: Brightness.light);
- SystemChrome.setSystemUIOverlayStyle(style);
- var size = MediaQuery.of(context).size;
- // final Controller c = Get.put(Controller());
- SizeFit.initialize(context);
- double min = 667.px; // 表盘和进食中尺寸判断,小于667则,用小表盘
- Global().circleWidth = 330.px;
- Global().paintWidth = 40.px;
- Global().progressWidth = 117.px;
- Global().scale = 1.0;
- if (size.height <= min) {
- Global().scale = 0.9;
- // Global().circleWidth = 280.px;
- // Global().paintWidth = 35.px;
- // Global().progressWidth = 99.px;
- }
- EdgeInsets safePadding = MediaQuery.of(context).padding;
- bottom = safePadding.bottom;
- Widget me = const MePage();
- return Scaffold(
- body: Stack(
- children: [
- if (showLogin == false)
- Opacity(
- opacity: tabIndex == 0 ? 1 : 0,
- child: const HomeScreen(),
- ),
- if (tabIndex == 1) me,
- if (showLogin) const Login(),
- if (showLogin == false)
- AnimatedPositioned(
- left: 0,
- bottom: bottomOffset,
- right: 0,
- duration: const Duration(milliseconds: 300),
- child: Opacity(
- opacity: animation0.value,
- child: Container(
- color: Colors.transparent,
- margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
- child: Container(
- width: double.infinity,
- height: 52.px,
- margin: EdgeInsets.fromLTRB(14.px, 0, 14.px, 14.px),
- decoration: const BoxDecoration(
- color: Color(0xFF142133),
- borderRadius: BorderRadius.all(Radius.circular(26))),
- child: Flex(
- direction: Axis.horizontal,
- children: [
- Expanded(
- child: GestureDetector(
- onTap: () {
- setState(() {
- tabIndex = 0;
- Global().homePage.getDatas();
- });
- },
- child: Center(
- child: Image.asset(
- tabIndex == 0
- ? 'assets/images/tab_home_sel.png'
- : 'assets/images/tab_home.png',
- width: 64.px,
- height: 40.px,
- ),
- ),
- )),
- Expanded(
- child: GestureDetector(
- onTap: () {
- setState(() {
- tabIndex = 1;
- });
- },
- child: Center(
- child: Image.asset(
- tabIndex == 1
- ? 'assets/images/tab_me_sel.png'
- : 'assets/images/tab_me.png',
- width: 64.px,
- height: 40.px,
- ),
- ),
- )),
- ],
- ),
- ),
- ),
- ),
- )
- ],
- ),
- );
- }
- }
|