intro_controller.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. EventHandler.report(EventId.event_01000);
  83. // 监听登录状态
  84. _loginSubscription= ever(AccountRepository.getInstance().isLogin, (bool isLogin) {
  85. if (isLogin) {
  86. // 登录后跳转新用户页
  87. Future.delayed(const Duration(milliseconds: 300), () {
  88. NewUserPage.start();
  89. });
  90. }
  91. });
  92. }
  93. /// 开始自动切页计时
  94. void _startAutoSwitchTimer() {
  95. _autoPageTimer?.cancel();
  96. final duration = pageList[currentPage.value].stayDuration;
  97. _autoPageTimer = Timer(duration, swiperIntro);
  98. }
  99. /// 执行切页
  100. void swiperIntro() {
  101. if (currentPage.value == pageList.length - 1) {
  102. pageController.value.jumpToPage(0);
  103. currentPage.value = 0;
  104. } else {
  105. pageController.value.nextPage(
  106. duration: const Duration(milliseconds: 500),
  107. curve: Curves.easeInOut,
  108. );
  109. currentPage.value++;
  110. }
  111. _startAutoSwitchTimer(); // 切完后,重新根据新页面设定计时
  112. }
  113. /// 页面切换时回调
  114. void onPageChanged(int index) {
  115. currentPage.value = index;
  116. _startAutoSwitchTimer();
  117. }
  118. void clickLogin() {
  119. Timer(const Duration(milliseconds: 500), () {
  120. if (AccountRepository.getInstance().isLogin.value) {
  121. ToastUtil.show("您已登录~");
  122. return;
  123. }
  124. LoginDialog.show();
  125. });
  126. EventHandler.report(EventId.event_01002);
  127. }
  128. void clickCustomButton() {
  129. Timer(const Duration(milliseconds: 500), () {
  130. NewUserPage.start();
  131. });
  132. EventHandler.report(EventId.event_01001);
  133. }
  134. void clickBack() {
  135. if (GetPlatform.isAndroid) {
  136. SystemNavigator.pop();
  137. }
  138. exit(0);
  139. }
  140. @override
  141. void onClose() {
  142. _loginSubscription.dispose();
  143. _autoPageTimer?.cancel();
  144. super.onClose();
  145. }
  146. }
  147. class PageBean {
  148. final WidgetFunction title;
  149. final String animUrl;
  150. final Duration stayDuration; // 每页停留时间
  151. PageBean({
  152. required this.title,
  153. required this.animUrl,
  154. required this.stayDuration,
  155. });
  156. }