我已经创建了调用showDialog的全局方法,每当我调用它时,如果我放置Navigator.pop(上下文),如果删除导航器,它就不会出现。如果没有导航器,我无法关闭错误对话框。我做错什么了吗?下面是我的代码
class GlobalMethod {
static void showErrorDialog(
{required String error, required BuildContext ctx}) {
showDialog(
context: ctx,
builder: (context) {
return AlertDialog(
title: Row(children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Icon(
Icons.logout,
color: Colors.grey,
size: 35,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text('Error Occured'),
),
]),
content: Text(error,
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontStyle: FontStyle.italic)),
actions: [
TextButton(
onPressed: () {
Navigator.canPop(context) ? Navigator.canPop(context) : null;
},
child: Text(
'Okay',
style: TextStyle(
color: Colors.red,
),
),
)
],
);
});
}当我调用该方法时,这就是一个例子。如果我删除Navigator.pop,错误对话框就会弹出,如果我把navigator.pop放出来,什么都不会出来。
else if (balance < price! ){
GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
Navigator.pop(context);
}发布于 2022-12-02 10:34:05
您需要在showErrorDialog之后删除Navigator:
else if (balance < price! ){
GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
}然后在您的showErrorDialog中更改这个
onPressed: () {
Navigator.canPop(context) ? Navigator.pop(context): null;
},您正在使用canPop,但是您需要使用pop进行导航,canPop只是返回一个bool结果。
发布于 2022-12-02 10:33:24
对于pop使用Navigator.pop(上下文)
class GlobalMethod {
static void showErrorDialog(
{required String error, required BuildContext ctx}) {
showDialog(
context: ctx,
builder: (context) {
return AlertDialog(
title: Row(children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Icon(
Icons.logout,
color: Colors.grey,
size: 35,
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text('Error Occured'),
),
]),
content: Text(error,
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontStyle: FontStyle.italic)),
actions: [
TextButton(
onPressed: () {
Navigator.canPop(ctx) ? Navigator.pop(context) : null;
},
child: Text(
'Okay',
style: TextStyle(
color: Colors.red,
),
),
)
],
);
});
}
}删除pop
else if (balance < price! ){
GlobalMethod.showErrorDialog(error: "you dont have enough balance , please top up first", ctx: context);
}https://stackoverflow.com/questions/74654180
复制相似问题