Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The device's exit button does not exit the app in flutter

i added willpopscope with back function In this project i added the exit code, but the app didn’t end; instead it went to the homepage and I made this initial screen to start the project with all the details running in the homepage screen.
I want to exit from device but its not working Can somebody tell me what the problem is?

@override
      Widget build(BuildContext context) {
    Future<bool> _back() {
        return showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: const Text("Do you want to exit this app?"),
                actions: <Widget>[
                  TextButton(
                    child: const Text('ok',style: TextStyle(fontSize: 15,color: Colors.indigo),),
                    onPressed: () {
                      Navigator.of(context).pop(true);
                    },
                  ),
    TextButton(
                    child: const Text('no',style: TextStyle(fontSize: 15,color: Colors.indigo),),
                    onPressed: () {
                      Navigator.of(context).pop(false);
                    },
                  ),
                ],
              );
            }
            ).then((value) => value ?? false);
      }
        return WillPopScope(
          onWillPop: _back,
          child: SafeArea(
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
                home: Scaffold(
              body: Container(
                  child: Padding(
                    padding: const EdgeInsets.all(80.0),
                    child: Column(
                      children: <Widget>[
                        Container(
                        Row(
                          children: <Widget>[
                            SizedBox(
                              height: 80,
                              width: 150,
                              child: OutlinedButton(
                                style: OutlinedButton.styleFrom(
                                    side: BorderSide(
                                        width: 3.0, color: Colors.indigo.shade900),
                                    backgroundColor: Colors.transparent,
                                    shape: const StadiumBorder()),
                                child: Text(
                                  "LETS START",
                                ),
                                onPressed: () {
          Navigator.of(context).push(MaterialPageRoute( builder: (context) =>  homepage(langname: '',)));                         },
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  )),
            )),
          ),
        );
      }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can do Like this:

  var currentBackPressTime;
  Future<bool> onWillPop() {
    DateTime now = DateTime.now();
    if (currentBackPressTime == null ||
        now.difference(currentBackPressTime) > Duration(seconds: 2)) {
      currentBackPressTime = now;
      Fluttertoast.showToast(msg: "Press Again to Exit!");
      return Future.value(false);
    } else {
      exit(0);
      // SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      return Future.value(false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: onWillPop,
      child: Scaffold( 

--------------
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading