onTap 到 AlertDialog flutter 2021

onTap to AlertDialog flutter 2021

我在 flutter 2021 版本中遇到了问题,因为当我搜索任何解决方案时,它都是旧的 无论如何,问题是我有一个带退出按钮的幻灯片菜单 当我单击该按钮时,必须显示对话框提醒用户选择是否要退出

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class MenuItems extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
   return Drawer(
     child: ListView(
       padding: EdgeInsets.zero,
       children: [
         UserAccountsDrawerHeader(
            accountName: Text("Admin",
              style: TextStyle(
                  fontSize: 25,
                  color: Colors.black87,
            ),
     ),
            accountEmail: Text("mail@mail.com",
              style: TextStyle(
                fontSize: 25,
                color: Colors.black87,
              ),
            ),
           currentAccountPicture: CircleAvatar(
             child: ClipOval(
               child: Image.asset("assets/images/me.jpg",
                 fit:BoxFit.cover,
               ),
             ),
           ),
           decoration: BoxDecoration(
             image: DecorationImage(
               image: AssetImage("assets/images/image.png"),
               fit: BoxFit.cover,
             )
           ),
        ),
         ListTile(
           leading:Icon(Icons.save),
           title: Text('Saved Results'),
           onTap: ()=>print("saved result"),
         ),
         Divider(),
         ListTile(
           leading:Icon(Icons.contact_page),
           title: Text('Contact Us'),
           onTap: ()=>print("Contact us"),
         ),
         ListTile(
           leading:Icon(Icons.info),
           title: Text('About US'),
           onTap: ()=>print("About us"),
         ),
         Divider(),
         ListTile(
           leading:Icon(Icons.exit_to_app),
           title: Text('Exit'),
           //the function doesn't proccess i don't know why
           onTap: ()=>showDialogWidget(context),
         ),

       ],
     ),
   );
  }
}
//this is the function to return alerDialog but it doesn't work why i dont know
 AlertDialog showDialogWidget(BuildContext context) {
  return AlertDialog(
     // when i did print("sth") it printed 
     title: Text("Are you sure?"),
     content: Text("Would you really want to exit the app?"),
     actions: [
       TextButton(onPressed: () {}, child: Text("Exit")),
       TextButton(onPressed: () {}, child: Text("Cancel")),
     ],
    elevation: 24.0,
    backgroundColor: Colors.green,
    shape: CircleBorder(),
   );
 }

所以请问我如何从幻灯片菜单中的按钮导航以及如何从幻灯片菜单中弹出警报消息 提前谢谢你

这个例子正是您想要做的

 ListTile(
                        title: Text(GlobalString().exit),
                        onTap: () {
                          return showDialog(
                              context: context,
                              builder: (context) => AlertDialog(
                                    title: Text(GlobalString().exitAnswer),
                                    actions: [
                                      TextButton(
                                          onPressed: () =>
                                              Navigator.pop(context, false),
                                          child: Text('No')),
                                      TextButton(
                                          onPressed: () {
                                            if (prefs.getString(
                                                    Constant().INPROCESS) !=
                                                '') {
                                              exit(0);
                                            } else {
                                              prefs.clear().whenComplete(() {
                                                dbHelper.deleteDatabase();
                                                exit(0);
                                              });
                                            }
                                          },
                                          child: Text('Si'))
                                    ],
                                  ));
                        },
                      )

您只是缺少 material.dart 中的 showDialog 方法(让自动导入):

AlertDialog showDialogWidget(BuildContext context) {
  return showDialog(
        context: context,
        builder: (newContext) {
          return AlertDialog(
     // when i did print("sth") it printed 
     title: Text("Are you sure?"),
     content: Text("Would you really want to exit the app?"),
     actions: [
       TextButton(onPressed: () {}, child: Text("Exit")),
       TextButton(onPressed: () {}, child: Text("Cancel")),
     ],
    elevation: 24.0,
    backgroundColor: Colors.green,
    shape: CircleBorder(),
   );
});
 }