mixin_stateless_lifecycle.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:router/observer/getx_router_observer.dart';
  5. import 'mixin_state_lifecycle.dart';
  6. /*
  7. * StateLessWidget 的生命周期实现基于 Getx 框架扩展实现
  8. */
  9. mixin StateLessLifecycleMixin on StatelessWidget implements RouteAware, WidgetsBindingObserver, IStateLifecycle {
  10. bool _lifecyclePerformed = false;
  11. void _ensureLifecycle(BuildContext context) {
  12. if (!_lifecyclePerformed) {
  13. final stateLessLifecycle = Get.put(StateLessLifecycle(), tag: key?.toString());
  14. stateLessLifecycle.setLifecycleCallback(MyLifecycleCallback(
  15. onReadyAction: () => _onReady(context),
  16. onCloseAction: _onClose,
  17. ));
  18. _lifecyclePerformed = true;
  19. }
  20. }
  21. void _onReady(BuildContext context) {
  22. final route = ModalRoute.of(context);
  23. if (route != null && route is PageRoute) {
  24. routeObserver.subscribe(this, route); // 订阅至RouteObserver
  25. }
  26. WidgetsBinding.instance.addObserver(this); // 添加至WidgetsBinding观察者
  27. }
  28. void _onClose() {
  29. routeObserver.unsubscribe(this); // 取消订阅RouteObserver
  30. WidgetsBinding.instance.removeObserver(this); // 移除WidgetsBinding观察者
  31. }
  32. @protected
  33. Widget buildWidget(BuildContext context);
  34. @override
  35. Widget build(BuildContext context) {
  36. _ensureLifecycle(context);
  37. return buildWidget(context);
  38. }
  39. @override
  40. void didChangeAppLifecycleState(AppLifecycleState state) {
  41. if (state == AppLifecycleState.resumed) {
  42. onResume();
  43. } else if (state == AppLifecycleState.paused) {
  44. onPause();
  45. }
  46. }
  47. @override
  48. void didPop() {
  49. onStop();
  50. }
  51. @override
  52. void didPopNext() {
  53. onResume();
  54. }
  55. @override
  56. void didPush() {
  57. onStart();
  58. }
  59. @override
  60. void didPushNext() {
  61. onPause();
  62. }
  63. // =========================== 默认空实现 ===========================
  64. @override
  65. void didChangeAccessibilityFeatures() {}
  66. @override
  67. void didChangeLocales(List<Locale>? locales) {}
  68. @override
  69. void didChangeMetrics() {}
  70. @override
  71. void didChangePlatformBrightness() {}
  72. @override
  73. void didChangeTextScaleFactor() {}
  74. @override
  75. void didHaveMemoryPressure() {}
  76. @override
  77. Future<bool> didPopRoute() {
  78. return Future<bool>.value(false);
  79. }
  80. @override
  81. Future<bool> didPushRoute(String route) {
  82. return Future<bool>.value(false);
  83. }
  84. @override
  85. Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
  86. return didPushRoute(routeInformation.location!);
  87. }
  88. @override
  89. Future<AppExitResponse> didRequestAppExit() async {
  90. return AppExitResponse.exit;
  91. }
  92. @override
  93. void onStart() {}
  94. @override
  95. void onStop() {}
  96. @override
  97. void onResume() {}
  98. @override
  99. void onPause() {}
  100. }
  101. /*
  102. * 继承自 DisposableInterface 实现 Getx 的生命周期
  103. */
  104. class StateLessLifecycle extends DisposableInterface {
  105. @override
  106. void onReady() {
  107. super.onReady();
  108. callback?.onReadyCallback();
  109. }
  110. @override
  111. void onClose() {
  112. callback?.onCloseCallback();
  113. super.onClose();
  114. }
  115. ILifecycleCallback? callback;
  116. void setLifecycleCallback(ILifecycleCallback callback) {
  117. this.callback = callback;
  118. }
  119. }
  120. // 自定义生命周期回调接口
  121. abstract class ILifecycleCallback {
  122. void onReadyCallback();
  123. void onCloseCallback();
  124. }
  125. class MyLifecycleCallback implements ILifecycleCallback {
  126. final VoidCallback onReadyAction;
  127. final VoidCallback onCloseAction;
  128. MyLifecycleCallback({required this.onReadyAction, required this.onCloseAction});
  129. @override
  130. void onReadyCallback() {
  131. onReadyAction();
  132. }
  133. @override
  134. void onCloseCallback() {
  135. onCloseAction();
  136. }
  137. }