view.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import 'dart:async';
  2. import 'package:fis_common/logger/logger.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. import 'package:vitalapp/architecture/app_parameters.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'package:vitalapp/store/store.dart';
  9. import 'controller.dart';
  10. class SplashPage extends GetView<SplashController> {
  11. const SplashPage({super.key});
  12. @override
  13. Widget build(BuildContext context) {
  14. return const ImageAnimation();
  15. }
  16. }
  17. class ImageAnimation extends StatefulWidget {
  18. const ImageAnimation({super.key});
  19. @override
  20. State<ImageAnimation> createState() => _ImageAnimationState();
  21. }
  22. class _ImageAnimationState extends State<ImageAnimation>
  23. with SingleTickerProviderStateMixin {
  24. late final SplashController _controller;
  25. late AnimationController _animationController;
  26. late Animation<double> _animation;
  27. int _callServerTimes = 0;
  28. ///是否本地工作站(若为True,则需等待Server启动后再结束Splash Page)
  29. bool _isLocalStation = false;
  30. // 定义Timer,用于轮询
  31. Timer? _serverCheckTimer;
  32. @override
  33. void initState() {
  34. super.initState();
  35. _isLocalStation = AppParameters.data.isLocalStation;
  36. if (_isLocalStation) {
  37. _serverCheckTimer = Timer.periodic(
  38. Duration(seconds: 1), (Timer t) => checkForConnected());
  39. }
  40. _controller = Get.find<SplashController>();
  41. final animationCompleter = Completer<void>();
  42. Future.wait([
  43. _controller.loadData(),
  44. animationCompleter.future,
  45. ]).then((_) {
  46. if (_isLocalStation) {
  47. return;
  48. }
  49. _nextPage();
  50. });
  51. // 创建动画控制器
  52. _animationController = AnimationController(
  53. duration: const Duration(milliseconds: 4200),
  54. vsync: this,
  55. );
  56. // 创建动画
  57. _animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
  58. // 启动动画
  59. _animationController.forward();
  60. _animationController.addStatusListener((status) {
  61. if (status == AnimationStatus.completed) {
  62. // 动画结束
  63. animationCompleter.complete();
  64. }
  65. });
  66. }
  67. @override
  68. void dispose() {
  69. // 销毁动画控制器
  70. _animationController.dispose();
  71. // 确保timer被释放
  72. _serverCheckTimer?.cancel();
  73. super.dispose();
  74. }
  75. void checkForConnected() async {
  76. // 这里替换成你的接口调用逻辑
  77. bool isCallSuccess = await callServer();
  78. if (isCallSuccess) {
  79. logger.i('call server success,times:${_callServerTimes}');
  80. // 如果接口调用成功,取消timer并且跳转到登录页面
  81. _serverCheckTimer?.cancel();
  82. _nextPage();
  83. }
  84. }
  85. Future<bool> callServer() async {
  86. try {
  87. await rpc.vinnoServer.echoAsync();
  88. return true;
  89. } catch (e) {
  90. _callServerTimes++;
  91. logger.i('call server times:${_callServerTimes}');
  92. if (kDebugMode) {
  93. print('call server times:${_callServerTimes}');
  94. }
  95. }
  96. return false;
  97. }
  98. @override
  99. Widget build(BuildContext context) {
  100. return Scaffold(
  101. backgroundColor: Theme.of(context).primaryColor,
  102. body: Center(
  103. child: AnimatedBuilder(
  104. animation: _animation,
  105. builder: (BuildContext context, Widget? child) {
  106. return Opacity(
  107. opacity: _animation.value,
  108. child: Container(
  109. alignment: Alignment.center,
  110. height: 200,
  111. child: Image.asset(
  112. 'assets/images/logo.png',
  113. fit: BoxFit.fitHeight,
  114. ),
  115. ),
  116. );
  117. },
  118. ),
  119. ),
  120. );
  121. }
  122. void _nextPage() {
  123. /// 重启判断是否是web端并且无token
  124. if (!kIsWeb || Store.user.token == null) {
  125. _controller.onRouteTo();
  126. }
  127. /// web端 并且有token
  128. if (kIsWeb && Store.user.token != null) {
  129. Get.offAllNamed("/");
  130. }
  131. }
  132. }