intro_controller.dart 4.7 KB

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