警告对话框按钮没有为 onwillPop 方法提供参数

Alert dialog button doesnt give parameter to the on willPop method

我想给 on will pop 方法提供参数来决定用户是否要退出,我使用了下面的方法..

Future<bool> showAlertDialog(BuildContext context){
    bool response;
    AlertDialog alert = AlertDialog(
      elevation: 24,
      content: Text("Çıkmak istediğinizden emin misiniz?"),
      actions: [
        TextButton(onPressed:(){
          response=true;
          return response;
          },
          child: Text("EVET")),
        TextButton(onPressed:(){
          response=false;
          return response;
          },
          child: Text("HAYIR")),
      ],
    );
    showDialog(
      context: context,
      builder: (_)=>alert);
  }

当用户按下 android 手机后退按钮时,警告框会看到我上面分享的内容,根据来自此方法的参数,应用程序将关闭或不关闭,为此我使用on 将显示如下所示的方法

onWillPop: ()async{
        var response = await showAlertDialog(context);
        return response;
      },

但是当用户点击“是”或“否”时什么也没有发生,我做错了什么,请帮助我

  1. 你应该return showDialog() 函数的布尔值

  2. 使用Navigator.of(context).pop(true);到return true & Navigator.of(context).pop(FALSE); to return false on button press.

        Future<bool> showAlertDialog(BuildContext context){
         AlertDialog alert = AlertDialog(
           elevation: 24,
           content: Text("Çıkmak istediğinizden emin misiniz?"),
           actions: [
             TextButton(onPressed:(){
               Navigator.of(context).pop(true);
             },
                 child: Text("EVET")),
             TextButton(onPressed:(){
    
               Navigator.of(context).pop(false);
    
             },
                 child: Text("HAYIR")),
           ],
         );
      return showDialog(
             context: context,
             builder: (_)=>alert);
       }
    

    .

您可以在 yes/no 选项的点击操作上使用回调。

这是代码..

typedef YesOrNoTapCallback = Function(bool);

提醒对话框中的一些变化

Future showAlertDialog(BuildContext context, YesOrNoTapCallback isYesTapped) async {
    AlertDialog alert = AlertDialog(
      elevation: 24,
      content: Text("Çıkmak istediğinizden emin misiniz?"),
      actions: [
        TextButton(onPressed:(){

          isYesTapped(true);
        },
            child: Text("EVET")),
        TextButton(onPressed:(){

          isYesTapped(false);
        },
            child: Text("HAYIR")),
      ],
    );
    await showDialog(
        context: context,
        builder: (_)=>alert);
  }

这就是你如何在 onWillPop 中使用它

 onWillPop: () async {
        bool isPop = false;
        await showAlertDialog(context, (alertResponse) {
          Navigator.of(context).pop(); // This will dismiss the alert dialog
          isPop = alertResponse;
        });
        return isPop;
      },