Home > OS >  How use alert dialog flutter
How use alert dialog flutter

Time:02-02

I try building alert function, but this error follow return:

Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.


  Future<void> _showAlertDialog() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          // <-- SEE HERE
          title: const Text('Cancel booking'),
          content: SingleChildScrollView(
            child: ListBody(
              children: const <Widget>[
                Text('Are you sure want to cancel booking?'),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('No'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('Yes'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

I would like to click on a certain elevatedbutton to open the alert dialog

CodePudding user response:

Try to pass the current BuildContext to your function

  Future<void> _showAlertDialog(BuildContext context) async {
  • Related