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 in Flutter by sarit
• 1,830 points
367 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 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 in Flutter by Anushi
267 views
0 votes
1 answer

How to create a circle icon button in Flutter?

To create a circle icon button in ...READ MORE

answered Mar 27 in Flutter by Tanishqa
• 1,170 points
5,468 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 in Flutter by vinayak
192 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 in Flutter by venky
1,210 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 in Flutter by pooja
691 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 in Flutter by Tej
3,226 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 in Flutter by vishalini
1,298 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