title: Drawer navigation that retains or restores state of page
body: <p>I'm building a Flutter app and I'm trying to wrap my head around Navigation and State. I built a very simple app below that has two pages that both have the increment button on them. They both share a <code>Scaffold</code> definition so that there is a consistent navigation drawer on both pages.</p>
  
  <p>Basically the functionality I want is that <code>FirstPage</code> and <code>SecondPage</code> are singletons. So if you increment the counter on <code>FirstPage</code> a few times, go to the <code>SecondPage</code> and then return to the <code>FirstPage</code> through the drawer (and not the back button), the <code>FirstPage</code> should still have its counter incremented.</p>
  
  <p>Right now if you do this it appears to create a new instance of <code>FirstPage</code> due to the <code>Navigation.push()</code>. Additionally if you're on the <code>FirstPage</code> and use the drawer to click "First Page" again for some reason, you should not lose state.</p>
  
  <p>I saw a Flutter dev on here mention the term "<a href="https://stackoverflow.com/questions/46259751/how-to-push-multiple-routes-with-flutter-navigator">non-linear navigation</a>" and that made me think something like this is possible. Thanks for your help.</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(title: "Navigation", home: FirstPage());
    }
  }
  
  class MyScaffold extends Scaffold {
    final BuildContext context;
    final Text title;
    final Widget body;
  
    MyScaffold({
      @required this.context,
      @required this.title,
      @required this.body,
    })  : assert(context != null),
          assert(title != null),
          assert(body != null),
          super(
              appBar: AppBar(title: title),
              drawer: Drawer(
                  child: ListView(padding: EdgeInsets.zero, children: &lt;Widget&gt;[
                SizedBox(height: 100.0),
                ListTile(
                    title: Text("First Page"),
                    onTap: () =&gt; Navigator.of(context).push(MaterialPageRoute(
                        builder: (BuildContext context) =&gt; FirstPage()))),
                ListTile(
                    title: Text("Second Page"),
                    onTap: () =&gt; Navigator.of(context).push(MaterialPageRoute(
                        builder: (BuildContext context) =&gt; SecondPage()))),
              ])),
              body: body);
  }
  
  class FirstPage extends StatefulWidget {
    @override
    _FirstPageState createState() =&gt; _FirstPageState();
  }
  
  class _FirstPageState extends State&lt;FirstPage&gt; {
    int _counter = 0;
    void _increment() =&gt; setState(() =&gt; _counter++);
  
    @override
    Widget build(BuildContext context) {
      return MyScaffold(
          context: context,
          title: Text("First Page"),
          body: Center(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: &lt;Widget&gt;[
                Text('$_counter'),
                RaisedButton(onPressed: _increment, child: Icon(Icons.add))
              ])));
    }
  }
  
  class SecondPage extends StatefulWidget {
    @override
    _SecondPageState createState() =&gt; _SecondPageState();
  }
  
  class _SecondPageState extends State&lt;SecondPage&gt; {
    int _counter = 0;
    void _increment() =&gt; setState(() =&gt; _counter++);
  
    @override
    Widget build(BuildContext context) {
      return MyScaffold(
          context: context,
          title: Text("Second Page"),
          body: Center(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: &lt;Widget&gt;[
                Text('$_counter'),
                RaisedButton(onPressed: _increment, child: Icon(Icons.add))
              ])));
    }
  }
  </code></pre>