Flutter&AlertDialog:加载后,我的应用程序不会显示警报对话框。即使在打印警报对话框之前和之后的两次打印,对话框也被跳过。为什么?请帮我做这个。
onTap: () async {
if (_formKey.currentState.validate() &&
_ratingStar > 0) {
setState(() {
_loading = true;
});
dynamic result =
await User_DatabaseService().uploadFeedback(
comment: review );
setState(() {
_loading = false;
});
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
Navigator.pop(context);
} else {
print('Sth wrong');
}
} else {
print('Not submit');
}
},
请看一下我的代码,告诉我出了什么问题。非常感谢。我期待着你的来信。
问题如下:
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
Navigator.pop(context);
} else {
print('Sth wrong');
}
您正在显示对话框,然后将其弹出。请确保添加Navigator.pop(上下文(方法,只有在您单击警报对话框上的按钮之后。因此,重写代码如下:
if (result) {
print('Before dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(6.0))),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(
vertical: 60, horizontal: 10),
child: Text(
//'Please rate with star',
'평가해 주셔서 감사합니다!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
alignment: Alignment.center,
height: 50,
//color: primaryColor,
child: Text(
AppLocalizations.of(context)
.translate('OKAY'),
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold),
),
),
),
],
),
);
},
);
print('After dialog');
} else {
print('Sth wrong');
}
问题出现在print('After dialog')
行之后的行中。您正在执行Navigator.pop(context);
,它基本上是从导航堆栈中删除对话框。
在颤动中:showDialog()
用于显示对话框。Navigator.pop(context)
用于删除对话框