颤振显示和隐藏警报对话框取决于动态值



在此代码段中,事件值是可以动态更改的流值。

LocationPermissions().serviceStatus.listen((event) {
if(event == ServiceStatus.disabled){
print('Location Disabled');
testAlert(context); // Show dialog
}else{
testAlert(context); //I want hide dialog when user enable location.How do?
print('Location Enabled');
}
}   

这是我的对话框代码。

void testAlert(BuildContext context){
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Location service disable"),
content: new Text("You must enable your location access"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Go Setting"),
onPressed: () {
openLocationSetting();
//visible ?Navigator.pop(context , true): Navigator.pop(context, false);
},
),
],
);
},
);
}

显示和隐藏方式取决于事件值。谢谢。

当用户启用location则不要调用对话框方法,只需删除此行testAlert(context);

LocationPermissions().serviceStatus.listen((event) {
if(event == ServiceStatus.disabled){
print('Location Disabled');
testAlert(context); // Show dialog
}else{
// testAlert(context); //remove or just comment this line 
print('Location Enabled');
}

}

更新:-

通过在此调用Navigator.pop()来消除平面按钮上的警报 对话框将被关闭

示例:-

void testAlert(BuildContext context){
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Location service disable"),
content: new Text("You must enable your location access"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Go Setting"),
onPressed: () {
Navigator.pop(context)
openLocationSetting();

},
),
],
);
},
);
}

在生命周期中调用它,即当用户再次返回应用程序时didChangeAppLifecycleState()方法

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// If user resumed to this app, check permission
if(state == AppLifecycleState.resumed) {
LocationPermissions().serviceStatus.listen((event) {
if(event == ServiceStatus.disabled){
print('Location Disabled');
testAlert(context); // Show dialog
}else{
testAlert(context); //I want hide dialog when user enable location.How do?
print('Location Enabled');
}
}  
}
}

最新更新