controller.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'package:flutter/gestures.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flyinsono/lab/manager/page_manager.dart';
  5. import 'package:fis_ui/utils/sizer/sizer.dart';
  6. import 'package:fis_common/index.dart';
  7. import 'package:get/get.dart';
  8. import 'index.dart';
  9. class LabMainController extends GetxController {
  10. LabMainController();
  11. static const tabViewId = "tab_view";
  12. static const headerLogoId = "header_logo";
  13. static const headerTabsId = "header_tabs";
  14. final state = LabMainState();
  15. late final PageManager pageManager;
  16. // Tab 栏滑动控制器
  17. final ScrollController tabScrollController = ScrollController();
  18. void scrollTabByPointerScrollEvent(PointerScrollEvent event) {
  19. // 如果超出边界,则设置为边界值
  20. if (tabScrollController.offset + event.scrollDelta.dy < 0) {
  21. tabScrollController.jumpTo(0);
  22. } else if (tabScrollController.offset + event.scrollDelta.dy >
  23. tabScrollController.position.maxScrollExtent) {
  24. tabScrollController.jumpTo(tabScrollController.position.maxScrollExtent);
  25. } else {
  26. tabScrollController
  27. .jumpTo(tabScrollController.offset + event.scrollDelta.dy);
  28. }
  29. }
  30. // tab 栏滚动监听
  31. void _onTabScroll() {
  32. _updateLeftRightMoreIcon();
  33. }
  34. // 更新左右两侧的更多图标
  35. void _updateLeftRightMoreIcon() {
  36. if (tabScrollController.offset > 0) {
  37. state.showLeftMoreIcon = true;
  38. } else {
  39. state.showLeftMoreIcon = false;
  40. }
  41. if (tabScrollController.position.maxScrollExtent > 0 &&
  42. tabScrollController.offset <
  43. tabScrollController.position.maxScrollExtent) {
  44. state.showRightMoreIcon = true;
  45. } else {
  46. state.showRightMoreIcon = false;
  47. }
  48. }
  49. void scrollTabToLeft() {
  50. tabScrollController.animateTo(
  51. 0,
  52. duration: Duration(milliseconds: 300),
  53. curve: Curves.ease,
  54. );
  55. }
  56. void scrollTabToRight() {
  57. tabScrollController.animateTo(
  58. tabScrollController.position.maxScrollExtent,
  59. duration: Duration(milliseconds: 300),
  60. curve: Curves.ease,
  61. );
  62. }
  63. void _onWindowResize(e) {
  64. WidgetsBinding.instance.addPostFrameCallback((_) {
  65. _updateLeftRightMoreIcon();
  66. });
  67. }
  68. void _onUpdateHeaderTabs(_, __) {
  69. update([tabViewId, headerTabsId, headerLogoId]);
  70. }
  71. void _onUpdateLeftRightMoreIcon(_, bool scrollToEnd) {
  72. WidgetsBinding.instance.addPostFrameCallback((_) {
  73. if (scrollToEnd) {
  74. if (tabScrollController.position.maxScrollExtent > 0) {
  75. tabScrollController.animateTo(
  76. tabScrollController.position.maxScrollExtent,
  77. duration: Duration(milliseconds: 300),
  78. curve: Curves.ease,
  79. );
  80. }
  81. }
  82. _updateLeftRightMoreIcon();
  83. });
  84. }
  85. // 设置底部自定义文本
  86. void setBottomCustomText(String text) {
  87. state.bottomCustomText = text;
  88. }
  89. /// 在 widget 内存中分配后立即调用。
  90. @override
  91. void onInit() {
  92. super.onInit();
  93. pageManager = PageManager.init();
  94. pageManager.initDefaultTabs();
  95. pageManager.tabChangeEvent.addListener(_onUpdateHeaderTabs);
  96. pageManager.updateLeftRightMoreIconEvent
  97. .addListener(_onUpdateLeftRightMoreIcon);
  98. tabScrollController.addListener(_onTabScroll);
  99. /// 监听resize事件
  100. Sizer.ins.addListener(_onWindowResize);
  101. /// 移动端强制横屏
  102. if (FPlatform.isIOS || FPlatform.isAndroid) {
  103. SystemChrome.setPreferredOrientations(
  104. [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
  105. }
  106. }
  107. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  108. @override
  109. void onReady() {
  110. super.onReady();
  111. }
  112. /// 在 [onDelete] 方法之前调用。
  113. @override
  114. void onClose() {
  115. super.onClose();
  116. tabScrollController.removeListener(_onTabScroll);
  117. Sizer.ins.removeListener(_onWindowResize);
  118. pageManager.tabChangeEvent.removeListener(_onUpdateHeaderTabs);
  119. pageManager.updateLeftRightMoreIconEvent
  120. .removeListener(_onUpdateLeftRightMoreIcon);
  121. pageManager.reset();
  122. }
  123. /// dispose 释放内存
  124. @override
  125. void dispose() {
  126. super.dispose();
  127. tabScrollController.removeListener(_onTabScroll);
  128. Sizer.ins.removeListener(_onWindowResize);
  129. pageManager.tabChangeEvent.removeListener(_onUpdateHeaderTabs);
  130. pageManager.updateLeftRightMoreIconEvent
  131. .removeListener(_onUpdateLeftRightMoreIcon);
  132. pageManager.reset();
  133. }
  134. }
  135. // 全局调用入口
  136. class LabMainHook {
  137. /// 设置底部自定义文本
  138. static setBottomCustomText(String text) {
  139. try {
  140. Get.find<LabMainController>().setBottomCustomText(text);
  141. } catch (e) {
  142. print(e);
  143. }
  144. }
  145. }