关键代码
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
print('应用程序可见并响应用户输入。');
break;
case AppLifecycleState.inactive:
print('应用程序处于非活动状态,并且未接收用户输入');
break;
case AppLifecycleState.paused:
print('用户当前看不到应用程序,没有响应');
break;
case AppLifecycleState.suspending:
print('应用程序将暂停。');
break;
default:
}
}
}
Flutter: 监听App显示,隐藏