tab_hook_mixin.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:flyinsono/lab/manager/page_manager.dart';
  2. import 'package:get/get.dart';
  3. /// Tab 页扩展功能,用于控制 tab 页的激活和休眠
  4. ///
  5. /// 使用方法,初始化控制器时候传入 controller 所在 page 的 hashId
  6. ///
  7. /// e.g.
  8. /// ```dart
  9. /// class LabTaskController extends GetxController with TabHookMixin {
  10. /// LabTaskController({required this.tabHashCode});
  11. ///
  12. /// @override
  13. /// int tabHashCode;
  14. /// ....
  15. /// }
  16. /// ```
  17. /// page 初始化时候传入 hashId
  18. /// ```dart
  19. /// class LabTaskPage extends GetView<LabTaskController> {
  20. /// const LabTaskPage({Key? key}) : super(key: key);
  21. ///
  22. /// @override
  23. /// Widget build(BuildContext context) {
  24. /// return GetBuilder<LabTaskController>(
  25. /// init: LabTaskController(tabHashCode: this.hashCode),
  26. /// .....
  27. /// ```
  28. mixin TabHookMixin on GetxController {
  29. /// 一般情况下传入当前页面的 hashCode (this.hashCode)
  30. int get tabHashCode;
  31. bool isTabActive = true;
  32. /// tab 页休眠时候调用
  33. void onSleep();
  34. /// tab 页激活时候调用(首次进入不调用)
  35. void onActive(dynamic arguments);
  36. void initTabStateListener() {
  37. // 此处检查 tabHashCode 是否初始化
  38. try {
  39. final int temp = tabHashCode;
  40. print("TabHookMixin init tabHashCode: $temp");
  41. } catch (e) {
  42. Get.log("tabHashCode 未初始化,请根据使用方法先初始化 tabHashCode");
  43. return;
  44. }
  45. try {
  46. PageManager.ins.tabChangeEvent.addListener(tabChangeHandler);
  47. isTabActive = PageManager.ins.activeTabHash == tabHashCode;
  48. } catch (e) {
  49. print("TabHookMixin init failed: $e");
  50. }
  51. }
  52. void removeTabStateListener() {
  53. try {
  54. PageManager.ins.tabChangeEvent.removeListener(tabChangeHandler);
  55. } catch (e) {
  56. print("TabHookMixin remove failed: $e");
  57. }
  58. }
  59. void tabChangeHandler(_, TabChangeRequest request) {
  60. try {
  61. if (tabHashCode == request.tabHash) {
  62. if (!isTabActive) {
  63. Get.log(
  64. 'GetxController instance with tag "$tabHashCode" now is active.');
  65. onActive(request.arguments);
  66. isTabActive = true;
  67. }
  68. } else {
  69. if (isTabActive) {
  70. Get.log(
  71. 'GetxController instance with tag "$tabHashCode" now goes to sleep');
  72. onSleep();
  73. isTabActive = false;
  74. }
  75. }
  76. } catch (e) {
  77. print("tabChangeHandler failed: $e");
  78. }
  79. }
  80. @override
  81. void onInit() {
  82. super.onInit();
  83. initTabStateListener();
  84. }
  85. @override
  86. void onReady() {
  87. super.onReady();
  88. if (isTabActive) {
  89. Get.log('GetxController instance with tag "$tabHashCode" now is active.');
  90. }
  91. }
  92. @override
  93. void onClose() {
  94. super.onClose();
  95. removeTabStateListener();
  96. }
  97. @override
  98. void dispose() {
  99. super.dispose();
  100. removeTabStateListener();
  101. }
  102. }