intro_controller.dart 3.8 KB

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