0

I have a problem to implement the third code here 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;
     },
   );
 }
}

the call off "showAlertDialog();" is still error because it require parameter typed "BuildContext"

If there are another ways to do it, please tell me, thank you very much

Alfan
  • 19
  • 5
  • Does this answer your question? [How to make an AlertDialog in Flutter?](https://stackoverflow.com/questions/53844052/how-to-make-an-alertdialog-in-flutter) – Raul Sauco Dec 19 '21 at 11:00
  • I think it's a different cases – Alfan Dec 19 '21 at 11:59
  • 1
    Sorry, maybe I wasn't clear, it seems like the direct answer to what you are asking would be [this question](https://stackoverflow.com/q/49100196/2557030). But, looking at your question, it seems like your end goal is learning how to display alert dialogs in Flutter, in that case, the question I linked above is a better resource than the one you sent. The Flutter team also released a video about BuildContext [here](https://www.youtube.com/watch?v=rIaaH87z1-g) If you want to get the example working, move the code that uses _context_ within a scope that has access to it, i.e. the build method. – Raul Sauco Dec 19 '21 at 15:09
  • I understand now, thank you very much – Alfan Dec 20 '21 at 03:30

1 Answers1

0

it turns out that I must place the code inside the Widget build(BuildContext context), but outside the return command, so I this works on me (I also modify the dialog's function)

 @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,
         ),
       )
     );
   }
Alfan
  • 19
  • 5