intro_controller.dart 3.7 KB

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