view.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/store/store.dart';
  6. import 'controller.dart';
  7. class SplashPage extends GetView<SplashController> {
  8. const SplashPage({super.key});
  9. @override
  10. Widget build(BuildContext context) {
  11. return const ImageAnimation();
  12. }
  13. }
  14. class ImageAnimation extends StatefulWidget {
  15. const ImageAnimation({super.key});
  16. @override
  17. State<ImageAnimation> createState() => _ImageAnimationState();
  18. }
  19. class _ImageAnimationState extends State<ImageAnimation>
  20. with SingleTickerProviderStateMixin {
  21. late final SplashController _controller;
  22. late AnimationController _animationController;
  23. late Animation<double> _animation;
  24. @override
  25. void initState() {
  26. super.initState();
  27. _controller = Get.find<SplashController>();
  28. final animationCompleter = Completer<void>();
  29. Future.wait([
  30. _controller.loadData(),
  31. animationCompleter.future,
  32. ]).then((_) {
  33. /// 重启判断是否是web端并且无token
  34. if (!kIsWeb || Store.user.token == null) {
  35. _controller.onRouteTo();
  36. }
  37. /// web端 并且有token
  38. if (kIsWeb && Store.user.token != null) {
  39. Get.offAllNamed("/");
  40. }
  41. });
  42. // 创建动画控制器
  43. _animationController = AnimationController(
  44. duration: const Duration(milliseconds: 4200),
  45. vsync: this,
  46. );
  47. // 创建动画
  48. _animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
  49. // 启动动画
  50. _animationController.forward();
  51. _animationController.addStatusListener((status) {
  52. if (status == AnimationStatus.completed) {
  53. // 动画结束
  54. animationCompleter.complete();
  55. }
  56. });
  57. }
  58. @override
  59. void dispose() {
  60. // 销毁动画控制器
  61. _animationController.dispose();
  62. super.dispose();
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. return Scaffold(
  67. backgroundColor: Theme.of(context).primaryColor,
  68. body: Center(
  69. child: AnimatedBuilder(
  70. animation: _animation,
  71. builder: (BuildContext context, Widget? child) {
  72. return Opacity(
  73. opacity: _animation.value,
  74. child: Container(
  75. alignment: Alignment.center,
  76. height: 200,
  77. child: Image.asset(
  78. 'assets/images/logo.png',
  79. fit: BoxFit.fitHeight,
  80. ),
  81. ),
  82. );
  83. },
  84. ),
  85. ),
  86. );
  87. }
  88. }