title: How to get Flutter ScrollController to save position of ListView.builder() when changing tabs?
body: <p>I have made a simple example with 2 tabs, each containing a ListView builder.  My goal is to be able to scroll in the first list view, switch to the 2nd tab, and then switch back to the first and see the same scroll position from before. </p>
  
  <p>I have tried adding Keys to each of the list views, but that was only a guess as I don't fully understand keys.  That didn't help.</p>
  
  <p>Why don't the ScrollControllers save the scroll position?</p>
  
  <p>Here is the example main.dart:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: HomeScreen(),
      );
    }
  }
  
  class HomeScreen extends StatefulWidget {
    @override
    _HomeScreenState createState() =&gt; _HomeScreenState();
  }
  
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    ScrollController controllerA = ScrollController(keepScrollOffset: true);
    ScrollController controllerB = ScrollController(keepScrollOffset: true);
    @override
    Widget build(BuildContext context) {
      return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: &lt;Widget&gt;[
                Text('controllerA'),
                Text('controllerB'),
              ],
            ),
          ),
          body: TabBarView(
            children: &lt;Widget&gt;[
              ListView.builder(
                  controller: controllerA,
                  itemCount: 2000,
                  itemBuilder: (context, i) {
                    return ListTile(
                        title: Text(
                      i.toString(),
                      textScaleFactor: 1.5,
                      style: TextStyle(color: Colors.blue),
                    ));
                  }),
              ListView.builder(
                  controller: controllerB,
                  itemCount: 2000,
                  itemBuilder: (context, i) {
                    return Card(
                      child: ListTile(
                        title: Text(i.toString()),
                      ),
                    );
                  }),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>Here is a hacky but working example of what I want.  This doesn't feel like the correct way to do this though, as its rebuilding both controllers every frame.</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: HomeScreen(),
      );
    }
  }
  
  class HomeScreen extends StatefulWidget {
    @override
    _HomeScreenState createState() =&gt; _HomeScreenState();
  }
  
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    double offsetA = 0.0;
    double offsetB = 0.0;
  
    @override
    Widget build(BuildContext context) {
      ScrollController statelessControllerA =
          ScrollController(initialScrollOffset: offsetA);
      statelessControllerA.addListener(() {
        setState(() {
          offsetA = statelessControllerA.offset;
        });
      });
  
      ScrollController statelessControllerB =
          ScrollController(initialScrollOffset: offsetB);
      statelessControllerB.addListener(() {
        setState(() {
          offsetB = statelessControllerB.offset;
        });
      });
  
      return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: &lt;Widget&gt;[
                Text('controllerA'),
                Text('controllerB'),
              ],
            ),
          ),
          body: TabBarView(
            children: &lt;Widget&gt;[
              ListView.builder(
                  controller: statelessControllerA,
                  itemCount: 2000,
                  itemBuilder: (context, i) {
                    return ListTile(
                        title: Text(
                      i.toString(),
                      textScaleFactor: 1.5,
                      style: TextStyle(color: Colors.blue),
                    ));
                  }),
              ListView.builder(
                  controller: statelessControllerB,
                  itemCount: 2000,
                  itemBuilder: (context, i) {
                    return Card(
                      child: ListTile(
                        title: Text(i.toString()),
                      ),
                    );
                  }),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>