【问题标题】:Flutter show and hide AlertDialog depend on dynamic valueFlutter 显示和隐藏 AlertDialog 取决于动态值
【发布时间】:2020-10-13 06:54:50
【问题描述】:

在这个 sn-p 事件中,值是可以动态变化的流值。

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

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

如何显示和隐藏取决于事件值。谢谢。

【问题讨论】:

  • 使用Navigator.pop(context)隐藏对话框
  • 在哪里添加?那个代码兄弟@Jide
  • 我写了一个答案。检查一下

标签: android ios flutter mobile widget


【解决方案1】:

当用户启用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');
         }
    }  
  }
}

【讨论】:

  • 先生,我做到了。如果我在用户启用定位服务时删除该行,如何显示检测对话框。
  • 您的回答意味着当用户单击 GoSetting 按钮对话框将关闭但用户不选择位置。我想关闭并显示取决于异步值
  • 更新了我的答案,当用户返回应用程序时,您需要检查恢复生命周期方法,再次检查位置是否启用,如果不显示对话框
猜你喜欢
  • 2013-02-02
  • 2021-01-17
  • 1970-01-01
  • 2012-04-04
  • 2020-01-01
  • 2011-03-20
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
相关资源
最近更新 更多