intro_controller.dart 4.5 KB

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