intro_controller.dart 4.2 KB

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