1

I have a flutter app in which when opened it opens the splash screen and then the home page.

when I'm in the home page and I click Android's back button nothing happens since I used this code:

new WillPopScope(
    onWillPop: () async => false,
    child:Directionality(
        textDirection: direction,
        child:...
   )
)

but I want the application to exit if I clicked the back button in Android from the home page.

is there's a way to achieve this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mrs.tat
  • 597
  • 2
  • 6
  • 17
  • 1
    Possible duplicate of [How To Override the “Back” button in Flutter?](https://stackoverflow.com/questions/49356664/how-to-override-the-back-button-in-flutter) – Ashish Jul 15 '19 at 10:05

1 Answers1

1

This is intended behaviour since WillPopScope overrides the navigation.

You should implement the navigation yourself

example:

Future<bool> _exitApp(BuildContext context) {
  return showDialog(
        context: context,
        child: new AlertDialog(
          title: new Text('Do you want to exit this application?'),
          content: new Text('We hate to see you leave...'),
          actions: <Widget>[
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: new Text('No'),
            ),
            new FlatButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: new Text('Yes'),
            ),
          ],
        ),
      ) ??
      false;
}
new WillPopScope(
    onWillPop: () async => _exitApp(context),
    child:Directionality(
        textDirection: direction,
        child:...
   )
)

Reference.

EDIT to exit the app you can add import 'dart:io'; abd use exit(0) function.

Ammar Hussein
  • 5,534
  • 5
  • 18
  • 37