0

I am developing an app that mainly just displays information from a web service to the user (info created by their admin). On the Drawer Menu there are 5 screens to display different information.

After the user has logged in, I use Navigator to move between screens by using pushReplacement. Meaning the current screen is always at the bottom of the screen stack. If the user presses the back button on their device, the app exists immediately.

How can I catch this, so I can ask the user if they want to log out, and then display the login screen?

Thanks for any suggestions.

Cheers,

Paul

Paul Coshott
  • 735
  • 1
  • 9
  • 16

1 Answers1

0

You can do this with the WillPopScope widget and changing the onWillPop parameter. The example I posted does something similar to what you want as it shows an alert dialog that can determine what will happen next. The FlatButton onPressed code probably isn't exactly what you're looking for and you'll have to handle that according to your implementation This is the example code is from @AironTark answer to this related question.

@override
Widget build(BuildContext context) {
  return WillPopScope(
    child: Scaffold(...),
    onWillPop: () => showDialog<bool>(
      context: context,
      builder: (c) => AlertDialog(
        title: Text('Warning'),
        content: Text('Do you really want to exit'),
        actions: [
          FlatButton(
            child: Text('Yes'),
            onPressed: () => Navigator.pop(c, true),
          ),
          FlatButton(
            child: Text('No'),
            onPressed: () => Navigator.pop(c, false),
          ),
        ],
      ),
    ),
  );
}
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52