Home > Software engineering >  Function dialog cannot close my flutter app
Function dialog cannot close my flutter app

Time:11-25

I want to close the app when the user press the back button. My code works perfectly like that:

Future<bool> _onWillPop() async{    
    return (await showDialog(    
      context: context,    
      builder: (context) => new AlertDialog(    
        title: "Exit App?",    
        content: "Are you sure?",    
        actions: <Widget>[    
          new TextButton(    
            onPressed: () => Navigator.of(context).pop(false),    
            child: "NO",    
          ),    
          new TextButton(    
            onPressed: () => Navigator.of(context).pop(true),    
            child: "YES",    
          ),    
        ],    
      ),    
    ));    
}    

If I define the same dialog inside a function:

void exitdialog(){    
    (the above dialog)
}    

and then call the function to the point I want to exit, if I press yes only the dialog is closing, and if I add another Navigator.of(context).pop(true) then a black screen occurs. Any thoughts? I want to define the dialog inside a function in order for my code to be clear.In conclusion the thing that I am trying to do is:

Future<bool> _onWillPop() async{      
    exitdialog();    
}    

exitdialog(){
    return (showDialog(    
      context: context,    
      builder: (context) => new AlertDialog(    
        title: "Exit App?",    
        content: "Are you sure?",    
        actions: <Widget>[    
          new TextButton(    
            onPressed: () => Navigator.of(context).pop(false),    
            child: "NO",    
          ),    
          new TextButton(    
            onPressed: () => Navigator.of(context).pop(true),    
            child: "YES",    
          ),    
        ],    
      ),    
    ));  
}

CodePudding user response:

Use this function (may help's you):

Future<bool> _onWillPop() async{
  return (await showDialog(
    context: context,
    builder: (context) => new AlertDialog(
      title: "Exit App?",
      content: "Are you sure?",
      actions: <Widget>[
        new TextButton(
          onPressed: () => Navigator.of(context).pop(false),
          child: "NO",
        ),
        new TextButton(
          onPressed: () => Platform.isAndroid ? SystemNavigator.pop() : exit(0),
          child: "YES",
        ),
      ],
    ),
  ));
}

exit(0) not recommended to use, for iOS you can use minimize_app plugin to minimize app instead of exit by this code:

MinimizeApp.minimizeApp();

CodePudding user response:

Try Below code hope its helpful to you. refer alert dialog here

use this package: import 'dart:io';

onWillPop() async {
      return (await showDialog(
        context: context,
        builder: (context) => new AlertDialog(
          title: Text("Exit App?"),
          content: Text("Are you sure?"),
          actions: <Widget>[
            new TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text('NO'),
            ),
            new TextButton(
              onPressed: () => exit(0),
              child: Text('YES'),
            ),
          ],
        ),
      ));
    }

Your Widget:

Center(
    child: TextButton(
      onPressed: onWillPop,
      child: Text('Press Me'),
    ),
  ),
  • Related