动态显示和隐藏AlertDialog取决于动态值

时间:2020-06-23 08:53:59

标签: android ios flutter mobile widget

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

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);

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

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

1 个答案:

答案 0 :(得分:0)

当用户启用location时,不要调用dialog方法,只需删除此行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');
         }
    }  
  }
}
相关问题