Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Dismiss a dialog after pushing to a new page

Tags:

flutter

I am currently creating a todo app where if I long pressed a task tile, it will pop up a show dialog. After redirecting to the edit page, you can edit and save the changes. However, once you are done, you will be redirected to the homepage but will be welcomed with the previous dialog How do I dismiss the dialog after you have been directed to the edit page?

Here is the code of the show dialog:

Future _showAlert(data) async{
return showDialog<Null>(
  context: context,
  barrierDismissible: true,
  builder: (BuildContext context) {
    return new AlertDialog(
      title: new Text('Would you like to make changes?', style: new TextStyle(fontSize: 17.0)),
      actions: <Widget>[
        new FlatButton(
          child: new Text('Edit'),
          onPressed: (){
            Navigator.push(context, new MaterialPageRoute(builder: (context) => new EditTodoPage(todo: data, todoID: selectedTodoId,)));
          },
        ),
        new FlatButton(
          child: new Text('Delete'),
          onPressed: (){
            _delete(selectedTodoId);
            Navigator.of(context).pop();
          },
        )
      ],
    );
  }
);

}

like image 297
Happy Haris Avatar asked May 06 '18 01:05

Happy Haris


1 Answers

Navigator.push returns a Future that completes after calling Navigator.pop on your edit page.

You can dismiss your dialog after that, like this:

onPressed: () {
  Navigator.push(/* ... */)
    .then((result) {
      Navigator.of(context).pop();
    });
},
like image 147
André Sousa Avatar answered Sep 23 '22 04:09

André Sousa