mixin_state_lifecycle.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:router/observer/getx_router_observer.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. /*
  7. * StatefulWidget 的生命周期实现基于原生实现
  8. */
  9. mixin StateLifecycle<T extends StatefulWidget> on State<T> implements RouteAware, WidgetsBindingObserver, IStateLifecycle {
  10. @override
  11. void initState() {
  12. super.initState();
  13. WidgetsBinding.instance.addObserver(this);
  14. }
  15. @override
  16. void didChangeDependencies() {
  17. super.didChangeDependencies();
  18. final route = ModalRoute.of(context);
  19. if (route != null && route is PageRoute) {
  20. routeObserver.subscribe(this, route);
  21. }
  22. }
  23. @override
  24. void dispose() {
  25. routeObserver.unsubscribe(this);
  26. WidgetsBinding.instance.removeObserver(this);
  27. super.dispose();
  28. }
  29. @override
  30. void didPop() {
  31. onStop();
  32. }
  33. @override
  34. void didPopNext() {
  35. onResume();
  36. }
  37. @override
  38. void didPush() {
  39. onStart();
  40. }
  41. @override
  42. void didPushNext() {
  43. onPause();
  44. }
  45. @override
  46. void didChangeAppLifecycleState(AppLifecycleState state) {
  47. if (state == AppLifecycleState.resumed) {
  48. onResume();
  49. } else if (state == AppLifecycleState.paused) {
  50. onPause();
  51. }
  52. }
  53. // =========================== 默认空实现 ===========================
  54. @override
  55. void didChangeAccessibilityFeatures() {}
  56. @override
  57. void didChangeLocales(List<Locale>? locales) {}
  58. @override
  59. void didChangeMetrics() {}
  60. @override
  61. void didChangePlatformBrightness() {}
  62. @override
  63. void didChangeTextScaleFactor() {}
  64. @override
  65. void didHaveMemoryPressure() {}
  66. @override
  67. Future<bool> didPopRoute() {
  68. return Future<bool>.value(false);
  69. }
  70. @override
  71. Future<bool> didPushRoute(String route) {
  72. return Future<bool>.value(false);
  73. }
  74. @override
  75. Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
  76. return didPushRoute(routeInformation.location!);
  77. }
  78. @override
  79. Future<AppExitResponse> didRequestAppExit() async {
  80. return AppExitResponse.exit;
  81. }
  82. // 3.24.0 新加的回调
  83. @override
  84. void didChangeViewFocus(ViewFocusEvent event) {}
  85. @override
  86. void handleCancelBackGesture() {}
  87. @override
  88. void handleCommitBackGesture() {}
  89. @override
  90. bool handleStartBackGesture(PredictiveBackEvent backEvent) {
  91. return false;
  92. }
  93. @override
  94. void handleUpdateBackGestureProgress(PredictiveBackEvent backEvent) {}
  95. // ============== 自定义实现 ============
  96. @override
  97. void onStart() {}
  98. @override
  99. void onStop() {}
  100. @override
  101. void onResume() {}
  102. @override
  103. void onPause() {}
  104. }
  105. // 自定义生命周期回调接口
  106. abstract class IStateLifecycle {
  107. void onStart(); //启动页面
  108. void onStop(); //结束页面
  109. void onResume(); //重新可见
  110. void onPause(); //被覆盖隐藏
  111. }