123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import 'dart:ui';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:router/observer/getx_router_observer.dart';
- import 'package:shared/utils/log_utils.dart';
- /*
- * StatefulWidget 的生命周期实现基于原生实现
- */
- mixin StateLifecycle<T extends StatefulWidget> on State<T> implements RouteAware, WidgetsBindingObserver, IStateLifecycle {
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addObserver(this);
- }
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- final route = ModalRoute.of(context);
- if (route != null && route is PageRoute) {
- routeObserver.subscribe(this, route);
- }
- }
- @override
- void dispose() {
- routeObserver.unsubscribe(this);
- WidgetsBinding.instance.removeObserver(this);
- super.dispose();
- }
- @override
- void didPop() {
- onStop();
- }
- @override
- void didPopNext() {
- onResume();
- }
- @override
- void didPush() {
- onStart();
- }
- @override
- void didPushNext() {
- onPause();
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- if (state == AppLifecycleState.resumed) {
- onResume();
- } else if (state == AppLifecycleState.paused) {
- onPause();
- }
- }
- // =========================== 默认空实现 ===========================
- @override
- void didChangeAccessibilityFeatures() {}
- @override
- void didChangeLocales(List<Locale>? locales) {}
- @override
- void didChangeMetrics() {}
- @override
- void didChangePlatformBrightness() {}
- @override
- void didChangeTextScaleFactor() {}
- @override
- void didHaveMemoryPressure() {}
- @override
- Future<bool> didPopRoute() {
- return Future<bool>.value(false);
- }
- @override
- Future<bool> didPushRoute(String route) {
- return Future<bool>.value(false);
- }
- @override
- Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
- return didPushRoute(routeInformation.location!);
- }
- @override
- Future<AppExitResponse> didRequestAppExit() async {
- return AppExitResponse.exit;
- }
- // 3.24.0 新加的回调
- @override
- void didChangeViewFocus(ViewFocusEvent event) {}
- @override
- void handleCancelBackGesture() {}
- @override
- void handleCommitBackGesture() {}
- @override
- bool handleStartBackGesture(PredictiveBackEvent backEvent) {
- return false;
- }
- @override
- void handleUpdateBackGestureProgress(PredictiveBackEvent backEvent) {}
- // ============== 自定义实现 ============
- @override
- void onStart() {}
- @override
- void onStop() {}
- @override
- void onResume() {}
- @override
- void onPause() {}
- }
- // 自定义生命周期回调接口
- abstract class IStateLifecycle {
- void onStart(); //启动页面
- void onStop(); //结束页面
- void onResume(); //重新可见
- void onPause(); //被覆盖隐藏
- }
|