Home > Software engineering >  The device's exit button does not exit the app in flutter
The device's exit button does not exit the app in flutter

Time:12-15

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: '',)));                         },
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  )),
            )),
          ),
        );
      }
    }

CodePudding user response:

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( 

--------------
  • Related