title: How to permit back button to exit app in flutter
body: <p>I am currently using WillPopScope to prevent back button after I watch tutorial in youtube and read some answers in stack overflow, the route is like this.. I have login screen and home screen.. when I am in home screen, when I click back button from my phone I want to close my app, I have tried using WillPopScope but when I click back button, it always navigates me to my login screen, is there a way so that my app will exit when I click back button? here is my code
  this is login screen </p>
  
  <pre><code>class _LoginScreenState extends State&lt;LoginScreen&gt; {
  postData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt('username', _username.text);
    Navigator.push(context,
                new MaterialPageRoute(
                    builder: (BuildContext context) =&gt;
                        new BottomTab()));
  }
  @override
    Widget build(BuildContext context) {
      return Column(
               children:&lt;Widget&gt;[
                 TextFormField(
                   controller: _username),
                 FlatButton(
                   child: Text("Login"),
                   onPressed: () {
                      postData(_username.text);}),];}
  }
  </code></pre>
  
  <p>and this is my Tab</p>
  
  <pre><code>class _BottomTab extends State&lt;BottomTab&gt; {
  Future&lt;bool&gt; _back() {
      return showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              content: Text('exit?'),
              actions: &lt;Widget&gt;[
                FlatButton(
                  child: Text('ok'),
                  onPressed: () {
                    Navigator.of(context).pop(true);
                  },
                ),
  FlatButton(
                  child: Text('no'),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                ),
              ],
            );
          });
    }
    int _selectedIndex = 0;
  
    _onTap(int index) {
      setState(() =&gt; _myindex = index);
    }
  
    final List&lt;Widget&gt; pages = [
      HomeScreen(),
      ProfileScreen(), 
    ];
  
    final PageStorageBucket bucket = PageStorageBucket();
  
    Widget _myNavigation(int selectedIndex) =&gt; BottomNavigationBar(
          onTap: _onTap,
          currentIndex: myindex,
          type: BottomNavigationBarType.fixed,
          items: const &lt;MyTab&gt;[
            MyTab(icon: Icon(Icons.home), title: Text('Home')),
            MyTab(icon: Icon(Icons.person), title: Text('Profile')),
          ],
        );
  
    @override
    Widget build(BuildContext context) {
      return FutureProvider&lt;String&gt;(
        create: (context) async {
          final prefs = await SharedPreferences.getInstance();
          return prefs.getString("username");
        },
        child: Scaffold(
          key: scaffoldKey,
          body: WillPopScope(
            onWillPop: back,
            child: PageStorage(
              child: pages[_selectedIndex],
              bucket: bucket,
            ),
          ),
          bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
        ),
      );
    }
  }
  </code></pre>