[英]How to open the dialog when the app is terminated - Flutter?
当应用程序终止时,我需要打开对话框。 我在WidgetsBindingObserver的帮助下检查了应用程序的生命周期
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if(state == AppLifecycleState.resumed){
print('app is resumed');
} else if(state == AppLifecycleState.inactive){
print('app is inactive');
} else if(state == AppLifecycleState.paused){
print('app is paused');
} else if(state == AppLifecycleState.detached) {
print('app is detached');
if(state == AppLifecycleState.resumed){
print('app is resumed');
}
SchedulerBinding.instance.addPostFrameCallback((_) => CustomDialogBox() );
}
}
就像当应用程序 state 变为AppLifecycleState.detached时,应用程序被终止,而当应用程序 state 再次变为AppLifecycleState.resumed以显示对话框,但无法这样做。
有没有不同的方法来实现这一目标?
编辑这是应用程序终止时显示的屏幕
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if(state == AppLifecycleState.resumed){
print('app is resumed');
} else if(state == AppLifecycleState.inactive){
print('app is inactive');
} else if(state == AppLifecycleState.paused){
print('app is paused');
} else if(state == AppLifecycleState.detached) {
print('app is detached');
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to approve of this message?'),
],
),
),
);
});
// SchedulerBinding.instance.addPostFrameCallback((_) => CustomDialogBox() );
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar()
)
}
}
日志猫
I/flutter ( 8810): app is inactive
I/flutter ( 8810): app is paused
I/flutter ( 8810): app is resumed
D/DecorView( 8810): onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@bc1e6a7[MainActivity]
I/flutter ( 8810): app is inactive
I/flutter ( 8810): app is paused
I/flutter ( 8810): app is detached
D/OnePlusJankManager( 8810): Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.example.postman/com.example.postman.MainActivity--- jank level = 1
当您恢复应用程序时,将showDialog
调用移至。 在应用程序已分离后,flutter 可能无法构建对话框。
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if(state == AppLifecycleState.resumed){
print('app is resumed');
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to approve of this message?'),
],
),
),
);
});
} else if(state == AppLifecycleState.inactive){
print('app is inactive');
} else if(state == AppLifecycleState.paused){
print('app is paused');
} else if(state == AppLifecycleState.detached) {
print('app is detached');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.