intro_controller.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/base/base_controller.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:keyboard/data/repository/account_repository.dart';
  7. import 'package:keyboard/utils/toast_util.dart';
  8. import '../../data/consts/constants.dart';
  9. import '../../resource/assets.gen.dart';
  10. import '../../dialog/login/login_dialog.dart';
  11. import '../../module/new_user/new_user_page.dart';
  12. import 'package:injectable/injectable.dart';
  13. @injectable
  14. class IntroController extends BaseController {
  15. final tag = "IntroController";
  16. Rx<PageController> pageController = PageController().obs;
  17. var currentPage = 0.obs;
  18. Timer? _autoPageTimer;
  19. IntroController();
  20. /// 页面数据列表
  21. final List<PageBean> pageList = [
  22. PageBean(
  23. title: Text.rich(
  24. textAlign: TextAlign.center,
  25. TextSpan(
  26. children: [
  27. TextSpan(
  28. text: '想知道\n',
  29. style: TextStyle(
  30. color: Colors.black.withAlpha(153),
  31. fontSize: 14.sp,
  32. fontWeight: FontWeight.w500,
  33. ),
  34. ),
  35. TextSpan(
  36. text: 'Ta ',
  37. style: TextStyle(
  38. color: const Color(0xFF7D46FC),
  39. fontSize: 24.sp,
  40. fontWeight: FontWeight.w600,
  41. ),
  42. ),
  43. TextSpan(
  44. text: '对你到底是‘朋友’还是‘心动’?',
  45. style: TextStyle(
  46. color: Colors.black.withAlpha(153),
  47. fontSize: 14.sp,
  48. fontWeight: FontWeight.w500,
  49. ),
  50. ),
  51. ],
  52. ),
  53. ),
  54. animUrl: Assets.anim.animIntroFirstData,
  55. stayDuration: const Duration(milliseconds:4100),
  56. ),
  57. PageBean(
  58. title: const SizedBox(),
  59. animUrl: Assets.anim.animIntroSecondData,
  60. stayDuration:const Duration(milliseconds: 5500)
  61. ),
  62. ];
  63. @override
  64. void onInit() {
  65. super.onInit();
  66. setFirstIntro(false);
  67. }
  68. @override
  69. void onReady() {
  70. super.onReady();
  71. _startAutoSwitchTimer();
  72. }
  73. /// 开始自动切页计时
  74. void _startAutoSwitchTimer() {
  75. _autoPageTimer?.cancel();
  76. final duration = pageList[currentPage.value].stayDuration;
  77. _autoPageTimer = Timer(duration, swiperIntro);
  78. }
  79. /// 执行切页
  80. void swiperIntro() {
  81. if (currentPage.value == pageList.length - 1) {
  82. pageController.value.jumpToPage(0);
  83. currentPage.value = 0;
  84. } else {
  85. pageController.value.nextPage(
  86. duration: const Duration(milliseconds: 500),
  87. curve: Curves.easeInOut,
  88. );
  89. currentPage.value++;
  90. }
  91. _startAutoSwitchTimer(); // 切完后,重新根据新页面设定计时
  92. }
  93. /// 页面切换时回调
  94. void onPageChanged(int index) {
  95. currentPage.value = index;
  96. _startAutoSwitchTimer();
  97. }
  98. void clickLogin() {
  99. Timer(const Duration(milliseconds: 500), () {
  100. if (AccountRepository.getInstance().isLogin.value) {
  101. ToastUtil.show("已登录登录");
  102. return;
  103. }
  104. LoginDialog.show();
  105. });
  106. }
  107. void clickCustomButton() {
  108. Timer(const Duration(milliseconds: 500), () {
  109. NewUserPage.start();
  110. });
  111. }
  112. @override
  113. void onClose() {
  114. _autoPageTimer?.cancel();
  115. super.onClose();
  116. }
  117. }
  118. class PageBean {
  119. final Widget title;
  120. final String animUrl;
  121. final Duration stayDuration; // 每页停留时间
  122. PageBean({
  123. required this.title,
  124. required this.animUrl,
  125. required this.stayDuration,
  126. });
  127. }