【问题标题】:onTap to AlertDialog flutter 2021onTap 到 AlertDialog 扑扑 2021
【发布时间】:2021-12-12 21:46:19
【问题描述】:

我在 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(),
   );
 }

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

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    您只是缺少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(),
       );
    });
     }
    

    【讨论】:

    • 如何在 android studio 中自动导入
    • 如果你写得足够慢 "showD" 它已经向你展示了原生 dart 方法,例如 showDialog。如果没有,则编写方法,当它带有红色下划线时,只需将鼠标悬停在其上,您就会看到导入或“更多操作”
    【解决方案2】:

    这个例子正是你想要做的

     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'))
                                        ],
                                      ));
                            },
                          )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 2021-11-18
      • 1970-01-01
      • 2020-03-16
      • 2021-12-08
      • 2021-07-09
      • 2017-10-11
      • 2014-11-04
      相关资源
      最近更新 更多