view.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'controller.dart';
  5. class SplashPage extends GetView<SplashController> {
  6. const SplashPage({super.key});
  7. @override
  8. Widget build(BuildContext context) {
  9. return const ImageAnimation();
  10. }
  11. }
  12. class ImageAnimation extends StatefulWidget {
  13. const ImageAnimation({super.key});
  14. @override
  15. State<ImageAnimation> createState() => _ImageAnimationState();
  16. }
  17. class _ImageAnimationState extends State<ImageAnimation>
  18. with SingleTickerProviderStateMixin {
  19. late final SplashController _controller;
  20. late AnimationController _animationController;
  21. late Animation<double> _animation;
  22. @override
  23. void initState() {
  24. super.initState();
  25. _controller = Get.find<SplashController>();
  26. final animationCompleter = Completer<void>();
  27. Future.wait([
  28. _controller.loadData(),
  29. animationCompleter.future,
  30. ]).then((_) {
  31. // 等待动画和数据加载,全部完成后跳转路由
  32. _controller.onRouteTo();
  33. });
  34. // 创建动画控制器
  35. _animationController = AnimationController(
  36. duration: const Duration(milliseconds: 4200),
  37. vsync: this,
  38. );
  39. // 创建动画
  40. _animation = Tween<double>(begin: 0, end: 1).animate(_animationController);
  41. // 启动动画
  42. _animationController.forward();
  43. _animationController.addStatusListener((status) {
  44. if (status == AnimationStatus.completed) {
  45. // 动画结束
  46. animationCompleter.complete();
  47. }
  48. });
  49. }
  50. @override
  51. void dispose() {
  52. // 销毁动画控制器
  53. _animationController.dispose();
  54. super.dispose();
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. return Scaffold(
  59. backgroundColor: Theme.of(context).primaryColor,
  60. body: Center(
  61. child: AnimatedBuilder(
  62. animation: _animation,
  63. builder: (BuildContext context, Widget? child) {
  64. return Opacity(
  65. opacity: _animation.value,
  66. child: Container(
  67. alignment: Alignment.center,
  68. // width: 200,
  69. height: 200,
  70. padding: const EdgeInsets.only(right: 80),
  71. child: Image.asset(
  72. 'assets/images/logo.png',
  73. fit: BoxFit.fitHeight,
  74. ),
  75. ),
  76. );
  77. },
  78. ),
  79. ),
  80. );
  81. }
  82. }