【问题标题】:showDialog gives errors in FluttershowDialog 在 Flutter 中给出错误
【发布时间】:2020-01-09 22:58:30
【问题描述】:

我尝试了 WillPopScope,但它根本不起作用。然后我尝试了 back_button_interceptor,它确实拦截了后退按钮。但是 showDialog 会引发错误。请帮忙?

class LoginState extends State<Login> {
  static String id = "Please scan ID card";
  static String user;
  int sc=0;

  @override
  initState() {
    super.initState();  BackButtonInterceptor.add(myInterceptor);
  }

  @override
  void dispose() {
    BackButtonInterceptor.remove(myInterceptor);
    super.dispose();
  }

  bool myInterceptor(bool stopDefaultButtonEvent) {
    print("BACK BUTTON!"); // Do some stuff.
    showDialog(
      context: context,
      child: new AlertDialog(
        title: new Text('Are you sure?'),
        content: new Text('App will exit'),
        actions: <Widget>[
          new FlatButton(
            onPressed: () => true,
            child: new Text('No'),
          ),
          new FlatButton(
            onPressed: () => false,
            child: new Text('Yes'),
          ),
        ],
      ),
    );
    return true;
  }


  @override
  Widget build(BuildContext context) {---}
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    如果您想让对话框在区域/对话框外点击时阻止关闭,请在 showDialog 中添加 barrierDismissible: false,

    然后您可以使用以下示例手动关闭该对话框,将其放在 AlertDialog 的操作中

                FlatButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: const Text(
                    'Cancel',
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  ),
                ),
    

    【讨论】:

    • 如果您打算在关闭对话框之前做其他事情,请将 () => Navigator.of(context).pop() 更改为 () => () { // 做事情在这里,然后在这个函数的末尾添加 Navigator.of(context).pop() }
    • 我正在尝试显示对话框。当我的拦截器执行时,我只是收到一堆错误。
    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 2022-11-03
    • 2021-12-24
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    相关资源
    最近更新 更多