앱화면이 꺼졌다가(백그라운드) 켜졌을때 어떻게 알수 있을까?
방법은 해당 페이지를 statefulWidget으로 바꾸고
initState(){}에 WidgetsBinding.instance.addObserver(this);를 해준뒤
didChangeAppLifecycleState(){} @override를 통해
앱state가 바뀔때마다 이벤트를 발생시킬수 있다.!
class Page extends StatefulWidget{
...생략
}
class PageState extends State<Page> with WidgetsBindingObserver{
AppLifecycleState? _notification;
@override
didChangeAppLifecycleState(AppLifecycleState state){
switch (state) {
case AppLifecycleState.resumed:
print("app in resumed");
break;
case AppLifecycleState.inactive:
print("app in inactive");
break;
case AppLifecycleState.paused:
print("app in paused");
break;
case AppLifecycleState.detached:
print("app in detached");
break;
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
잘 작동한다