185 lines
6.6 KiB
Dart
Executable File
185 lines
6.6 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/blue/blue_manage.dart';
|
||
import 'package:star_lock/flavors.dart';
|
||
import 'package:star_lock/main/lockMian/lockMain/lockMain_logic.dart';
|
||
import 'package:star_lock/tools/appFirstEnterHandle.dart';
|
||
import 'package:star_lock/tools/app_manager.dart';
|
||
import 'package:star_lock/tools/bindings/app_binding.dart';
|
||
import 'package:star_lock/tools/change_language_format.dart';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
|
||
import 'package:star_lock/translations/app_dept.dart';
|
||
import 'package:star_lock/translations/trans_lib.dart';
|
||
|
||
import 'appRouters.dart';
|
||
import 'baseWidget.dart';
|
||
import 'tools/appRouteObserver.dart';
|
||
|
||
import 'tools/store_service.dart';
|
||
|
||
class MyApp extends StatefulWidget {
|
||
MyApp({required this.isLogin, GlobalKey? key}) : super(key: key);
|
||
bool isLogin;
|
||
|
||
@override
|
||
State<MyApp> createState() => _MyAppState();
|
||
}
|
||
|
||
class _MyAppState extends State<MyApp> with WidgetsBindingObserver, BaseWidget {
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return ScreenUtilInit(
|
||
designSize: const Size(585, 1265),
|
||
builder: (BuildContext w, Widget? a) => _initMaterialApp());
|
||
}
|
||
|
||
GetMaterialApp _initMaterialApp() {
|
||
//判断是不是登录
|
||
String initialRoute;
|
||
if (widget.isLogin) {
|
||
initialRoute = Routers.starLockMain;
|
||
} else {
|
||
initialRoute = Routers.login;
|
||
}
|
||
return GetMaterialApp(
|
||
// enableLog: false,
|
||
title: F.navTitle,
|
||
navigatorObservers: <NavigatorObserver>[
|
||
AppRouteObserver().routeObserver
|
||
],
|
||
translations: TranslationMessage(),
|
||
supportedLocales: appDept.deptSupportedLocales,
|
||
localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
|
||
GlobalMaterialLocalizations.delegate,
|
||
GlobalCupertinoLocalizations.delegate,
|
||
GlobalWidgetsLocalizations.delegate,
|
||
],
|
||
// localeResolutionCallback用于在启动时动态确定应该使用哪种语言和地区。
|
||
localeResolutionCallback:
|
||
(Locale? locale, Iterable<Locale> supportedLocales) {
|
||
if (widget.isLogin) {
|
||
// 登录之后调用存储的语言
|
||
if (StoreService.to.getLanguageCode()!.isNotEmpty) {
|
||
locale = appDept.deptSupportedLocales
|
||
.where((Locale element) =>
|
||
element.toString() == StoreService.to.getLanguageCode())
|
||
.first;
|
||
} else {
|
||
// 如果没储存则调用系统的语言
|
||
locale = Get.deviceLocale;
|
||
}
|
||
} else {
|
||
// 没登录之前调用系统的语言
|
||
locale = Get.deviceLocale;
|
||
}
|
||
locale = ChangeLanguageFormat.convertLocale(locale!);
|
||
return supportedLocales.contains(locale)
|
||
? locale
|
||
: supportedLocales.first;
|
||
},
|
||
// 用来指定应用当前使用的语言和地区。它定义了应用在启动时加载的默认语言环境。
|
||
locale: StoreService.to.getLanguageCode()!.isNotEmpty
|
||
? appDept.deptSupportedLocales
|
||
.where((Locale element) =>
|
||
element.toString() == StoreService.to.getLanguageCode())
|
||
.first
|
||
: Get.deviceLocale,
|
||
// locale: Get.deviceLocale,
|
||
// fallbackLocale用于指定在无法找到匹配的语言时,应用应该使用的默认语言和地区。
|
||
fallbackLocale: Get.deviceLocale,
|
||
theme: ThemeData(
|
||
scaffoldBackgroundColor: const Color(0xFFF6F6F6),
|
||
backgroundColor: const Color(0xFFF6F6F6),
|
||
primaryColor: const Color(0xFFFFFFFF),
|
||
textTheme: TextTheme(
|
||
//用在非Material组件上的文字显示,
|
||
bodyText1:
|
||
TextStyle(fontSize: 28.sp, color: const Color(0xff2E2B2B)),
|
||
//Material组件上的文字显示
|
||
bodyText2:
|
||
TextStyle(fontSize: 28.sp, color: const Color(0xff2E2B2B)),
|
||
button: TextStyle(fontSize: 28.sp)),
|
||
iconTheme: IconThemeData(size: 28.sp, color: const Color(0xff2E2B2B)),
|
||
appBarTheme: AppBarTheme(
|
||
backgroundColor: const Color(0xFFFFFFFF),
|
||
elevation: 0,
|
||
centerTitle: true,
|
||
iconTheme:
|
||
IconThemeData(color: const Color(0xff333333), size: 36.sp),
|
||
titleTextStyle: TextStyle(
|
||
color: const Color(0xff333333),
|
||
fontWeight: FontWeight.w400,
|
||
fontSize: 36.sp),
|
||
),
|
||
splashColor: Colors.transparent,
|
||
// 点击时的高亮效果设置为透明
|
||
highlightColor: Colors.transparent,
|
||
),
|
||
debugShowCheckedModeBanner: false,
|
||
getPages: AppRouters.routePages,
|
||
builder: EasyLoading.init(),
|
||
initialBinding: AppBindings(),
|
||
initialRoute: initialRoute);
|
||
}
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
WidgetsBinding.instance.addObserver(this);
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
WidgetsBinding.instance.removeObserver(this);
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||
super.didChangeAppLifecycleState(state);
|
||
// AppLog.log('App--->state:$state');
|
||
switch (state) {
|
||
case AppLifecycleState.inactive:
|
||
// AppLog.log('App--->进入非活动状态');
|
||
break;
|
||
case AppLifecycleState.paused:
|
||
// AppLog.log('App--->进入后台');
|
||
BlueManage().disconnect();
|
||
break;
|
||
case AppLifecycleState.resumed:
|
||
// AppLog.log('App--->进入前台');
|
||
if (Get.isRegistered<LockMainLogic>()) {
|
||
//进入前台刷新如果存在锁列表则刷新,顺便判断 token 是否过期
|
||
Get.find<LockMainLogic>().updateZoneOffsetsAndLanguages();
|
||
}
|
||
break;
|
||
case AppLifecycleState.detached:
|
||
// AppLog.log('App--->挂起');
|
||
break;
|
||
case AppLifecycleState.hidden:
|
||
// AppLog.log('App--->隐藏');
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<bool> getLoginStatus() async {
|
||
final String? data = await Storage.getString(saveUserLoginData);
|
||
if (data != null && data.isNotEmpty) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
Future<void> getAgreePrivacyShowUpdate() async {
|
||
final String? data = await Storage.getString(isAgreePrivacy);
|
||
if (data == isAgreePrivacy) {
|
||
AppFirstEnterHandle().getAppFirstEnter(isShowUpdateVersion);
|
||
}
|
||
}
|