How to dismiss an AlertDialog on a FlatButton click

0 votes

I have the following AlertDialog.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: _dismissDialog,
                ),
              ],
            ),
        );

How can I make _dismissDialog() dismiss said AlertDialog?

Mar 24, 2023 in Flutter by sarit
• 1,830 points
2,762 views

1 answer to this question.

0 votes

To dismiss the AlertDialog when the FlatButton is clicked, you can use the Navigator.pop method in the _dismissDialog function. Here's an updated version of your code:

showDialog(
  context: context,
  child: AlertDialog(
    title: const Text("Location disabled"),
    content: const Text(
      "Location is disabled on this device. Please enable it and try again.",
    ),
    actions: [
      FlatButton(
        child: const Text("Ok"),
        onPressed: _dismissDialog,
      ),
    ],
  ),
);

void _dismissDialog() {
  Navigator.pop(context); // This will dismiss the dialog
}

In this code, the _dismissDialog function calls Navigator.pop(context), which will pop the top-most route off the navigator's stack. In this case, the top-most route is the AlertDialog, so calling Navigator.pop will dismiss it.

I hope this helps!

answered Mar 26, 2023 by pooja

Related Questions In Flutter

0 votes
1 answer

How to make a TextField with HintText but no Underline?

You can achieve this by using the ...READ MORE

answered Mar 21, 2023 in Flutter by Anushi
6,852 views
0 votes
2 answers

How to create a circle icon button in Flutter?

To create something similar to a FloatingActionButton, ...READ MORE

answered Aug 23, 2023 in Flutter by anonymous
• 140 points
14,367 views
0 votes
1 answer

Linking 3 Flutter Projects in One Flutter Project

Yes, it is possible to combine multiple ...READ MORE

answered Mar 20, 2023 in Flutter by vinayak
725 views
0 votes
1 answer

How can I change the app display name build with Flutter?

Yes, you can change the app display ...READ MORE

answered Mar 21, 2023 in Flutter by venky
2,605 views
0 votes
1 answer

3d Model In Flutter with GLB, OBJ, or GLTF file

To integrate a 3D model into Flutter ...READ MORE

answered Apr 13, 2023 in Flutter by pooja
1,890 views
0 votes
1 answer
0 votes
1 answer

How do you change the value inside of a textfield flutter?

To change the value inside a TextField ...READ MORE

answered Mar 26, 2023 in Flutter by Tej
8,927 views
0 votes
1 answer

How to implement drop down list in flutter?

You're close! The error you're getting is ...READ MORE

answered Mar 26, 2023 in Flutter by vishalini
4,092 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP