我不想当应用程序位于前台时显示通知。
如何检查我的应用程序的实时状态?
在您的 State<...> 类中,您需要实现WidgetsBindingObserver接口并监听小部件状态变化。像这样的事情:
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
AppLifecycleState? _notification;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() {
_notification = state;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addObserver(this);
...
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
}
然后当你想知道状态是什么时,检查
_notification.index property. _notification == null => no state changes happened,
0 - resumed,
1 - inactive,
2 - paused.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)