app_mobile.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:async';
  2. import 'package:auto_route/auto_route.dart';
  3. import 'package:double_back_to_exit/double_back_to_exit.dart';
  4. import 'package:easy_localization/easy_localization.dart';
  5. import 'package:firebase_messaging/firebase_messaging.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:quick_notify_2/quick_notify.dart';
  9. import 'package:simple_connection_checker/simple_connection_checker.dart';
  10. import 'package:telnow_mobile_new/main.dart';
  11. import 'package:telnow_mobile_new/src/injector/injector.dart';
  12. import 'package:telnow_mobile_new/src/layouts/components/template.dart';
  13. import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';
  14. import 'package:telnow_mobile_new/src/utils/U.dart';
  15. class MobAppPage extends StatefulWidget {
  16. final Widget child;
  17. const MobAppPage(this.child, {super.key});
  18. @override
  19. State<MobAppPage> createState() => _MobAppPageState();
  20. }
  21. class _MobAppPageState extends State<MobAppPage>with TickerProviderStateMixin {
  22. final NotificationClass notification = NotificationClass();
  23. late AnimationController _animationController;
  24. StreamSubscription? subscription;
  25. bool isConnected = true;
  26. bool begin = false;
  27. @override
  28. void initState() {
  29. if(!kIsWeb){
  30. _animationController = new AnimationController(duration: Duration(seconds: 2), vsync: this);
  31. _animationController.repeat(reverse: true);
  32. checkConnection();
  33. }
  34. else{
  35. U.getServerVersion();
  36. // getOtherList();
  37. begin = true;
  38. }
  39. // TODO: implement initState
  40. super.initState();
  41. }
  42. @override
  43. void dispose() {
  44. if(!kIsWeb) {
  45. subscription?.cancel();
  46. _animationController.dispose();
  47. }
  48. // TODO: implement dispose
  49. super.dispose();
  50. }
  51. checkConnection() async{
  52. await U.getServerVersion();
  53. // getOtherList();
  54. SimpleConnectionChecker _simpleConnectionChecker = SimpleConnectionChecker();
  55. subscription = _simpleConnectionChecker.onConnectionChange.listen((connected) {
  56. U.setInternetStatus(connected);
  57. setState(()=>begin = true);
  58. if(connected){
  59. setState(()=>isConnected = true);
  60. notification.initFirebaseMessaging(context);
  61. var valLang = U.retValidLang();
  62. var langCode = context.locale.toString();
  63. var code;
  64. if(valLang.indexOf(langCode) > 1){
  65. code = valLang.indexOf(langCode) - 1;
  66. }
  67. notification.startNotification(context, code: code);
  68. if(U.newServerVersion(1709864293)){
  69. U.checkPendingRequest(context);
  70. }
  71. if(ModalRoute.of(context)?.isCurrent != true) navigateBack(context);
  72. }
  73. else{
  74. Future.delayed(Duration(seconds: 4), (){
  75. if(!U.getInternetStatus()){
  76. handlingError(context, 0);
  77. setState(()=>isConnected = false);
  78. }
  79. });
  80. }
  81. });
  82. }
  83. getOtherList(){
  84. U.getOtherLabelList(context);
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. return Scaffold(
  89. body: Stack(
  90. alignment: Alignment.bottomCenter,
  91. children: [
  92. begin ? widget.child : Container(),
  93. isConnected?Container():IgnorePointer(
  94. child: FadeTransition(opacity: _animationController, child: Container(
  95. width: double.infinity, color: Colors.red,
  96. margin: EdgeInsets.only(bottom: 56),
  97. padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
  98. child: Text('noInternetTitle'.tr(), style: TextStyle(color: Colors.white), textAlign: TextAlign.center),
  99. )),
  100. )
  101. ],
  102. ),
  103. );
  104. }
  105. }
  106. //---------------------------------------------------------------------------------------------------------------
  107. class MobMenuTemplate extends StatefulWidget {
  108. final Widget child;
  109. const MobMenuTemplate({required this.child, Key? key}) : super(key: key);
  110. @override
  111. State<MobMenuTemplate> createState() => _MobMenuTemplateState();
  112. }
  113. class _MobMenuTemplateState extends State<MobMenuTemplate> {
  114. final SharedPreferencesManager sharedPreferencesManager = locator<SharedPreferencesManager>();
  115. bool historyMark = false;
  116. getUser() async {
  117. if(kIsWeb){
  118. if(!await QuickNotify.hasPermission()){
  119. await QuickNotify.requestPermission();
  120. }
  121. }
  122. if(sharedPreferencesManager.isKeyExists(SharedPreferencesManager.keyHistoryMark)!){
  123. setState(() => historyMark = sharedPreferencesManager.getBool(SharedPreferencesManager.keyHistoryMark)!);
  124. }
  125. FirebaseMessaging.onMessage.listen((event) {
  126. if(event.data['type'] != 'MESSAGE'){
  127. if(!context.router.currentUrl.endsWith("history") && mounted){
  128. setState(() => historyMark = true);
  129. sharedPreferencesManager.putBool(SharedPreferencesManager.keyHistoryMark, true);
  130. }
  131. }
  132. });
  133. }
  134. @override
  135. void initState() {
  136. getUser();
  137. super.initState();
  138. }
  139. @override
  140. Widget build(BuildContext context) {
  141. var index = 0;
  142. if(context.router.currentUrl.endsWith("history")){
  143. index = 1;
  144. historyMark = false;
  145. sharedPreferencesManager.putBool(SharedPreferencesManager.keyHistoryMark, false);
  146. }else if(context.router.currentUrl.endsWith("account")){
  147. index = 2;
  148. }
  149. return Scaffold(
  150. backgroundColor: backgroundColor,
  151. body: !kIsWeb?DoubleBackToExit(
  152. snackBarMessage: 'pressAgain'.tr(),
  153. child: widget.child,
  154. ):widget.child,
  155. bottomNavigationBar: BottomNavigationBar(
  156. backgroundColor: Colors.white,
  157. selectedItemColor: textColor,
  158. unselectedItemColor: textColor,
  159. type: BottomNavigationBarType.fixed,
  160. onTap: (value) {
  161. var pid = U.getPidFromUrl(context.router.currentUrl);
  162. if(value!=index) {
  163. switch (value) {
  164. case 0:
  165. context.router.removeLast();
  166. context.navigateToPath("/app/$pid/menu/home");
  167. break;
  168. case 1:
  169. context.router.removeLast();
  170. context.navigateToPath("/app/$pid/menu/history");
  171. break;
  172. default:
  173. context.router.removeLast();
  174. context.navigateToPath("/app/$pid/menu/account");
  175. break;
  176. }
  177. }
  178. },
  179. currentIndex: index,
  180. showUnselectedLabels: true,
  181. selectedFontSize: 12,
  182. items: <BottomNavigationBarItem>[
  183. BottomNavigationBarItem(icon: U.iconsax(index == 0 ? 'bold/home' : 'home-1', color: index==0 ? primaryColor : Color(0xff292D32).withValues(alpha: 0.5)), label: 'menuHome'.tr()),
  184. BottomNavigationBarItem(icon: Center(
  185. child: Stack(
  186. alignment: Alignment.topRight,
  187. children: [
  188. U.iconsax(index == 1 ? 'bold/archive-2' : 'archive-2', color: index==1 ? primaryColor : Color(0xff292D32).withValues(alpha: 0.5)),
  189. Container(
  190. width: historyMark?11:0, height: historyMark?11:0,
  191. decoration: BoxDecoration(color: Colors.red, borderRadius: BorderRadius.all(Radius.circular(50))),
  192. )
  193. ],
  194. ),
  195. ), label: 'menuHistory'.tr()),
  196. BottomNavigationBarItem(icon: U.iconsax(index == 2 ? 'bold/personalcard' : 'personalcard', color: index==2 ? primaryColor : Color(0xff292D32).withValues(alpha: 0.5)), label: 'menuAccount'.tr()),
  197. ],
  198. ),
  199. );
  200. }
  201. }