123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import 'dart:async';
- import 'package:fis_common/logger/logger.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/app_parameters.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'controller.dart';
- class SplashPage extends GetView<SplashController> {
- const SplashPage({super.key});
- @override
- Widget build(BuildContext context) {
- return const ImageAnimation();
- }
- }
- class ImageAnimation extends StatefulWidget {
- const ImageAnimation({super.key});
- @override
- State<ImageAnimation> createState() => _ImageAnimationState();
- }
- class _ImageAnimationState extends State<ImageAnimation>
- with SingleTickerProviderStateMixin {
- late final SplashController _controller;
- late AnimationController _animationController;
- late Animation<double> _animation;
- int _callServerTimes = 0;
- ///是否本地工作站(若为True,则需等待Server启动后再结束Splash Page)
- bool _isLocalStation = false;
- // 定义Timer,用于轮询
- Timer? _serverCheckTimer;
- @override
- void initState() {
- super.initState();
- _isLocalStation = AppParameters.data.isLocalStation;
- if (_isLocalStation) {
- _serverCheckTimer = Timer.periodic(
- Duration(seconds: 1), (Timer t) => checkForConnected());
- }
- _controller = Get.find<SplashController>();
- final animationCompleter = Completer<void>();
- Future.wait([
- _controller.loadData(),
- animationCompleter.future,
- ]).then((_) {
- if (_isLocalStation) {
- return;
- }
- _nextPage();
- });
- // 创建动画控制器
- _animationController = AnimationController(
- duration: const Duration(milliseconds: 4200),
- vsync: this,
- );
- // 创建动画
- _animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
- // 启动动画
- _animationController.forward();
- _animationController.addStatusListener((status) {
- if (status == AnimationStatus.completed) {
- // 动画结束
- animationCompleter.complete();
- }
- });
- }
- @override
- void dispose() {
- // 销毁动画控制器
- _animationController.dispose();
- // 确保timer被释放
- _serverCheckTimer?.cancel();
- super.dispose();
- }
- void checkForConnected() async {
- // 这里替换成你的接口调用逻辑
- bool isCallSuccess = await callServer();
- if (isCallSuccess) {
- logger.i('call server success,times:${_callServerTimes}');
- // 如果接口调用成功,取消timer并且跳转到登录页面
- _serverCheckTimer?.cancel();
- _nextPage();
- }
- }
- Future<bool> callServer() async {
- try {
- await rpc.vinnoServer.echoAsync();
- return true;
- } catch (e) {
- _callServerTimes++;
- logger.i('call server times:${_callServerTimes}');
- if (kDebugMode) {
- print('call server times:${_callServerTimes}');
- }
- }
- return false;
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Theme.of(context).primaryColor,
- body: Center(
- child: AnimatedBuilder(
- animation: _animation,
- builder: (BuildContext context, Widget? child) {
- return Opacity(
- opacity: _animation.value,
- child: Container(
- alignment: Alignment.center,
- height: 200,
- child: Image.asset(
- 'assets/images/logo.png',
- fit: BoxFit.fitHeight,
- ),
- ),
- );
- },
- ),
- ),
- );
- }
- void _nextPage() {
- /// 重启判断是否是web端并且无token
- if (!kIsWeb || Store.user.token == null) {
- _controller.onRouteTo();
- }
- /// web端 并且有token
- if (kIsWeb && Store.user.token != null) {
- Get.offAllNamed("/");
- }
- }
- }
|