@override void initState() { super.initState(); //注册 WidgetsBinding.instance.addObserver(this); }@override void dispose() { super.dispose(); //注销 WidgetsBinding.instance.removeObserver(this); } //主要检测生命周期 @override void didChangeAppLifecycleState(AppLifecycleState state) { //每次唤醒进行检查挨app更新 if (state == AppLifecycleState.resumed) { checkAppVersion(); } }
【flutter 检测生命周期 进行相关操作】
class FlutterLifeCycleState extends State
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
//添加观察者
}///生命周期变化时回调
//resumed:应用可见并可响应用户操作
//inactive:用户可见,但不可响应用户操作
//paused:已经暂停了,用户不可见、不可操作
//suspending:应用被挂起,此状态IOS永远不会回调
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print("@@@@@@@@@didChangeAppLifecycleState: $state");
//每次唤醒进行检查更新
if (state == AppLifecycleState.resumed) {
checkAppVersion();
}
}///当前系统改变了一些访问性活动的回调
@override
void didChangeAccessibilityFeatures() {
super.didChangeAccessibilityFeatures();
print("@@@@@@@@@ didChangeAccessibilityFeatures");
}/// Called when the system is running low on memory.
///低内存回调
@override
void didHaveMemoryPressure() {
super.didHaveMemoryPressure();
print("@@@@@@@@@ didHaveMemoryPressure");
}/// Called when the system tells the app that the user's locale has
/// changed. For example, if the user changes the system language
/// settings.
///用户本地设置变化时调用,如系统语言改变
@override
void didChangeLocales(List locale) {
super.didChangeLocales(locale);
print("@@@@@@@@@ didChangeLocales");
}/// Called when the application's dimensions change. For example,
/// when a phone is rotated.
///应用尺寸改变时回调,例如旋转
@override
void didChangeMetrics() {
super.didChangeMetrics();
Size size = WidgetsBinding.instance.window.physicalSize;
print("@@@@@@@@@ didChangeMetrics:宽:${size.width} 高:${size.height}");
}/// {@macro on_platform_brightness_change}
@override
void didChangePlatformBrightness() {
super.didChangePlatformBrightness();
print("@@@@@@@@@ didChangePlatformBrightness");
}///文字系数变化
@override
void didChangeTextScaleFactor() {
super.didChangeTextScaleFactor();
print(
"@@@@@@@@@ didChangeTextScaleFactor:${WidgetsBinding.instance.window.textScaleFactor}");
}@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text("flutter"),
),
);
}@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
//销毁观察者
}
}