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
• 5,950 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
• 14,372 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
• 20,031 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
• 3,314 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
• 5,932 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
• 4,334 views
0 votes
1 answer

Is there any way to write native apps for Windows, Blackberry, Android and Apple devices using Python?

Hi, This is an amazing Python framework just ...READ MORE

answered Jan 11, 2019 in Python by Nymeria
• 3,560 points
• 2,216 views
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
• 13,136 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
• 7,326 views