title: flutter: how to handle back button press when using navigator with pages
body: <p>I am trying to implement Navigator 2.0 method. The only thing I can't make to work is the back button presses. When I press the back button, the app just exits and not going to the previous page. I tried the WillPopPage widget but back button bypassing it.</p>
  <p>here is a sample code that doesn't work on back presses.</p>
  <pre><code>import 'package:flutter/material.dart';
  
  main(List&lt;String&gt; args) {
    runApp(TestApp());
  }
  
  class TestApp extends StatefulWidget {
    @override
    _TestAppState createState() =&gt; _TestAppState();
  }
  
  class _TestAppState extends State&lt;TestApp&gt; {
    bool go = false;
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Navigator(
          pages: [
            // page 1
            MaterialPage(
              child: W1(onTapped: () {
                setState(() =&gt; go = true);
              }),
            ),
            // page 2
            if (go) MaterialPage(child: W2()),
          ],
          onPopPage: (route, result) {
            if (!route.didPop(result)) return false;
            go = false;
            return true;
          },
        ),
      );
    }
  }
  
  class W1 extends StatelessWidget {
    final void Function() onTapped;
  
    const W1({Key key, this.onTapped}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: FlatButton(
            onPressed: onTapped,
            child: Text(&quot;Go to second&quot;),
          ),
        ),
      );
    }
  }
  
  class W2 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Text(&quot;pressing the back button is exiting the app..&quot;),
        ),
      );
    }
  }
  </code></pre>
  <p>I have already written a lot of code with this approach, so I can't go back to the imperative navigation anymore. How to handle the back pres with this approach?</p>