intro_controller.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/utils/toast_util.dart';
  11. import '../../data/consts/constants.dart';
  12. import '../../resource/assets.gen.dart';
  13. import '../../dialog/login/login_dialog.dart';
  14. import '../../module/new_user/new_user_page.dart';
  15. import 'package:injectable/injectable.dart';
  16. import '../../widget/platform_util.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. }
  77. /// 开始自动切页计时
  78. void _startAutoSwitchTimer() {
  79. _autoPageTimer?.cancel();
  80. final duration = pageList[currentPage.value].stayDuration;
  81. _autoPageTimer = Timer(duration, swiperIntro);
  82. }
  83. /// 执行切页
  84. void swiperIntro() {
  85. if (currentPage.value == pageList.length - 1) {
  86. pageController.value.jumpToPage(0);
  87. currentPage.value = 0;
  88. } else {
  89. pageController.value.nextPage(
  90. duration: const Duration(milliseconds: 500),
  91. curve: Curves.easeInOut,
  92. );
  93. currentPage.value++;
  94. }
  95. _startAutoSwitchTimer(); // 切完后,重新根据新页面设定计时
  96. }
  97. /// 页面切换时回调
  98. void onPageChanged(int index) {
  99. currentPage.value = index;
  100. _startAutoSwitchTimer();
  101. }
  102. void clickLogin() {
  103. Timer(const Duration(milliseconds: 500), () {
  104. if (AccountRepository.getInstance().isLogin.value) {
  105. ToastUtil.show("您已登录~");
  106. return;
  107. }
  108. if (PlatformUtil.isIOS) {
  109. LoginPage.start();
  110. } else {
  111. LoginDialog.show();
  112. }
  113. });
  114. }
  115. void clickCustomButton() {
  116. Timer(const Duration(milliseconds: 500), () {
  117. NewUserPage.start();
  118. });
  119. }
  120. void clickBack() {
  121. if (PlatformUtil.isAndroid) {
  122. SystemNavigator.pop();
  123. }
  124. exit(0);
  125. }
  126. @override
  127. void onClose() {
  128. _autoPageTimer?.cancel();
  129. super.onClose();
  130. }
  131. }
  132. class PageBean {
  133. final Widget title;
  134. final String animUrl;
  135. final Duration stayDuration; // 每页停留时间
  136. PageBean({
  137. required this.title,
  138. required this.animUrl,
  139. required this.stayDuration,
  140. });
  141. }