【问题标题】:Flutter AlertDialog Navigator pop black screen issueFlutter AlertDialog Navigator 弹出黑屏问题
【发布时间】:2019-05-09 12:13:36
【问题描述】:

我有一个奇怪的问题,AlertDialog 在颤动中关闭对话框。我正在使用下面的代码 sn-p 来关闭颤振文档中提到的对话框。

 Navigator.of(dialogContext).pop();

但显示它如何不起作用并使应用程序进入非活动模式并变成黑屏窗口。为了让它再次工作,我必须终止应用程序并重新启动。

这里是flutter中alertdialog的完整代码

Future<Null> _showDialogContactDial(context, Contact contactRecord) async {

  return showDialog<Null>(
    context: context,
    barrierDismissible: true, // user must tap button!
    builder: (BuildContext dialogContext) {
      return new AlertDialog(
        title: new Text('Confirm Number'),
        content: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new TextFormField(
                maxLines: 1,
                decoration: new InputDecoration(hintText: 'Number'),
                keyboardType: TextInputType.number,
                autofocus: false,
                initialValue: contactRecord.phoneNumber.number,
              ),
            ],
          ),
        ),
        actions: <Widget>[
          new FlatButton(
            child: new Text(
              'Call',
              style: TextStyle(color: Colors.black),
            ),
            onPressed: () {
              Navigator.of(dialogContext).pop();


              _launchURL(
                  context);
            },
          ),
          new FlatButton(
            color: Colors.red,
            child: new Text('Close', style: TextStyle(color: Colors.white)),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          ),
        ],
      );
    },
  );
}

我还注意到它适用于一个按钮“呼叫”而没有任何问题,但不适用于取消警报对话框,正如您在两个按钮操作中的相同代码 sn-p 中看到的那样。

帮助将不胜感激。

【问题讨论】:

  • 添加截图供参考
  • 这里的问题不是 AlertDialogBox - 它与调用它的父小部件有关。你的代码工作正常。两者都适用于呼叫和关闭操作小部件。

标签: android mobile dart flutter flutter-layout


【解决方案1】:

在您的对话框中。用 Builder 环绕你的平面按钮。

Builder(
          builder: (context) => FlatButton(
            child: Text('Cancelar'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ),

【讨论】:

    【解决方案2】:

    只需添加rootNavigator:true

    Navigator.of(dialogcon, rootNavigator: true).pop();
    

    【讨论】:

    • 你能补充解释吗?
    • 这一定是公认的答案.. 兄弟,你救了我的命!.. 非常感谢.. 但你能向我们解释一下为什么我们必须添加这个吗?
    • 非常感谢,节省了很多时间
    【解决方案3】:

    这在我的应用程序中有效,我对您的代码进行了一些更改,希望这可能会有所帮助,如果这对您没有帮助,那么我认为这是 _launchURl 方法中的问题。

    void _showDialogContactDial(BuildContext context, Contact contactRecord){
    
    showDialog<Null>(
    context: context,
    barrierDismissible: true, // user must tap button!
    builder: (BuildContext dialogContext) {
      return new AlertDialog(
        title: new Text('Confirm Number'),
        content: new SingleChildScrollView(
          child: new ListBody(
            children: <Widget>[
              new TextFormField(
                maxLines: 1,
                decoration: new InputDecoration(hintText: 'Number'),
                keyboardType: TextInputType.number,
                autofocus: false,
                initialValue: contactRecord.phoneNumber.number,
              ),
            ],
          ),
        ),
        actions: <Widget>[
          new FlatButton(
            child: new Text(
              'Call',
              style: TextStyle(color: Colors.black),
            ),
            onPressed: () {
              Navigator.of(dialogContext).pop();
    
    
              _launchURL(
                  context);
            },
          ),
          new FlatButton(
            color: Colors.red,
            child: new Text('Close', style: TextStyle(color: Colors.white)),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          ),
        ],
      );
    },
    

    ); }

    将此方法用作 onTap 的回调或您使用它的任何地方。

    【讨论】:

    • 如果你看到它就是 alertdialog 上下文。我尝试使用 Navigator.pop() 但我遇到了同样的问题。
    • 你能发布完整的代码吗?控制台(stacktrace)中是否有任何问题?请也发布。
    • 现在可以查看了吗?我已经更新了对话框的完整代码
    • 对我来说它看起来像相同的代码。是的,我删除了 _launchURL 方法。但它不起作用。
    • 您尝试从您的方法/函数中删除 Future 返回类型和异步?
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多