[英]What is "BuildContext" in flutter means?, and how to use it?
我在此处执行第三个代码时遇到问题https://www.codegrepper.com/search.php?answer_removed=1&q=alert%20dialog%20aler%20dialog%20flutter
class _HomepageState extends State<Homepage> {
@override
void initState() {
super.initState();
setState(() {
showAlertDialog();
});
}
@override
Widget build(BuildContext context) {
return ListView(
padding: EdgeInsets.all(20),
children: <Widget>[
Center(
child: Text(
'Welcome',
style: TextStyle(fontSize: 30),
textAlign: TextAlign.center,
),
),
SizedBox(height: 30),
Text(
"News",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
],
);
}
showAlertDialog(BuildContext context) {
// set up the button
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("My title"),
content: Text("This is my message."),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}
取消“showAlertDialog();” 仍然是错误,因为它需要类型为“BuildContext”的参数
如果还有其他的方法,请告诉我,非常感谢
事实证明,我必须将代码放在 Widget 构建(BuildContext 上下文)中,但在返回命令之外,所以我这对我有用(我还修改了对话框的功能)
@override
Widget build(BuildContext context) {
if (int.parse(COUNTDAY) >= 5) {
Future.delayed(Duration.zero, () => showAlert(context));
}
return ListView(
padding: EdgeInsets.all(20),
children: <Widget>[
Center(
child: Text(
'Welcome',
style: TextStyle(fontSize: 30),
textAlign: TextAlign.center,
),
),
SizedBox(height: 30),
Text(
"News",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
],
);
}
showAlert(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
"Your time is up",
style: TextStyle(fontSize: 25),
textAlign: TextAlign.center,
),
)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.