RouteLifecycleEvent enum
路由生命周期Mixin
提供路由生命周期监听功能,自动管理订阅和清理 支持两种使用方式:
- 通过 override 方法(推荐)
- 通过 Reactive Stream(支持链式操作)
使用方式:
// 1. 在 MaterialApp 中配置全局观察器
MaterialApp(
navigatorObservers: [LifecycleUtils.globalRouteObserver],
...
)
// 2. 在路由页面中使用 Mixin(无需任何设置)
class _MyPageState extends State<MyPage> with RouteLifecycleMixin {
// 方式1: 通过 override 方法(推荐)
@override
void onRoutePushed() {
super.onRoutePushed();
print('路由已推送 - 当前页面被推送到导航栈');
}
@override
void onRoutePushNext() {
super.onRoutePushNext();
print('进入下一个页面 - 从当前页面导航到下一个页面');
}
@override
void onRoutePopNext() {
super.onRoutePopNext();
print('回到当前页面 - 从下一个页面返回到当前页面');
}
@override
void onRoutePopped() {
super.onRoutePopped();
print('退出当前页面 - 当前页面从导航栈弹出');
}
// 方式2: 通过 Reactive Stream(支持链式操作)
@override
void initState() {
super.initState();
// 方式2.1: 使用通用方法 listenRouteEvent(推荐)
listenRouteEvent(
predicate: (event) => event == RouteLifecycleEvent.pushed,
onData: (event) => print('路由已推送: $event'),
);
// 方式2.2: 使用快捷方法(更简洁)
listenRoutePushed(onData: () {
print('路由已推送');
});
listenRoutePopNext(onData: () {
print('回到当前页面');
});
listenRouteEntered(onData: () {
print('进入当前页面(推送或返回)');
});
listenRouteExited(onData: () {
print('退出当前页面(进入下一页或弹出)');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(...);
}
}
路由生命周期事件类型
Values
- pushed → const RouteLifecycleEvent
-
路由已推送(进入当前页面)
- pushNext → const RouteLifecycleEvent
-
路由已推送下一个(进入下一个页面)
- popNext → const RouteLifecycleEvent
-
路由已弹出下一个(回到当前页面)
- popped → const RouteLifecycleEvent
-
路由已弹出(退出当前页面)
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- index → int
-
A numeric identifier for the enumerated value.
no setterinherited
- name → String
-
Available on Enum, provided by the EnumName extension
The name of the enum value.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
-
values
→ const List<
RouteLifecycleEvent> - A constant List of the values in this enum, in order of their declaration.