flutter 中的 "BuildContext" 是什么意思?如何使用?

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 上下文)中,但放在 return 命令之外,所以这对我有效(我还修改了对话框的功能)

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