intro_controller.dart 3.3 KB

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