import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:vitalapp/store/store.dart'; import 'controller.dart'; class SplashPage extends GetView { const SplashPage({super.key}); @override Widget build(BuildContext context) { return const ImageAnimation(); } } class ImageAnimation extends StatefulWidget { const ImageAnimation({super.key}); @override State createState() => _ImageAnimationState(); } class _ImageAnimationState extends State with SingleTickerProviderStateMixin { late final SplashController _controller; late AnimationController _animationController; late Animation _animation; @override void initState() { super.initState(); _controller = Get.find(); final animationCompleter = Completer(); Future.wait([ _controller.loadData(), animationCompleter.future, ]).then((_) { /// 重启判断是否是web端并且无token if (!kIsWeb || Store.user.token == null) { _controller.onRouteTo(); } /// web端 并且有token if (kIsWeb && Store.user.token != null) { Get.offAllNamed("/"); } }); // 创建动画控制器 _animationController = AnimationController( duration: const Duration(milliseconds: 4200), vsync: this, ); // 创建动画 _animation = Tween(begin: 0, end: 1).animate(_animationController); // 启动动画 _animationController.forward(); _animationController.addStatusListener((status) { if (status == AnimationStatus.completed) { // 动画结束 animationCompleter.complete(); } }); } @override void dispose() { // 销毁动画控制器 _animationController.dispose(); super.dispose(); } @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, ), ), ); }, ), ), ); } }