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

因此,请告诉我如何从幻灯片菜单中的按钮导航,以及如何从幻灯片中弹出警报消息提前感谢

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

这个例子正是您想要做的

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

最新更新