skip to Main Content

What i need is that when user clicking on home and recent button first alert must come "do you want to exit the app" then again tapping once more app must be closed

i tried using app life cycle but not working properly

2

Answers


  1. You can do it using PopScope. Where you can prevent system back gestures using canPop.

    you can also use onPopInvoked parameter if you want.

    To use PopScope just wrap your Widget with it and show AlertDialog:

    PopScope(
         canPop: false,
         onPopInvoked: (bool didPop) {},
         child: TextButton(),
    ),
    

    For more information about PopScope you can check documentation here: PopScope

    Login or Signup to reply.
  2. WillPopScope is deprecated after v3.12.0

    Widget build(BuildContext context) {
      return new WillPopScope(
        onWillPop: () async => false,
        child: new Scaffold(
          appBar: new AppBar(
            title: new Text("data"),
            leading: new IconButton(
              icon: new Icon(Icons.ac_unit),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ),
        ),
      );
    }
    

    You can use the new PopScope class (Flutter SDK 3.16.x or greater)

    class PageOne extends StatelessWidget {
      const PageOne({super.key});
    
      @override
      Widget build(BuildContext context) {
        return PopScope(
          onPopInvoked: (_) async => false,
          child: Scaffold(
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                Navigator.of(context).pushNamed('/pageTwo');
              }
            )
          )
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search