title: flutter multiple pageview and controller
body: <p>I have two pageviews:</p>
  
  <ul>
  <li>pageview0 has 3 pages: 1, 2, 3</li>
  <li>pageview1 has 3 pages: A, B, C</li>
  </ul>
  
  <p>Only one pageview is shown at a time, and I want to switch between them when I press a 'switch' button on AppBar.</p>
  
  <p>What I want:</p>
  
  <p>If I'm at pageview0 page2 and press the switch button,
  then I want to see pageview1 page A.</p>
  
  <p>But the below code shows pageview1 page B (the page with same index).
  I also tried to use two PageControllers instead of one, but the behavior is same.
  How can I control what page to show when there's multiple PageViews and switching between them?</p>
  
  <p>My code:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(home: MyHomePage());
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    List&lt;String&gt; pageview0 = ["1", "2", "3"];
    List&lt;String&gt; pageview1 = ["A", "B", "C"];
    int pageViewIndex = 0;
  
    // Try single controller.
    PageController controller;
    // Try multiple controller.
    List&lt;PageController&gt; controllers;
  
    @override
    void initState() {
      super.initState();
      controller = PageController(initialPage: 0);  // pageview0 and pageview1
      controllers = [];
      controllers.add(PageController(initialPage: 0));  // pageview0
      controllers.add(PageController(initialPage: 0));  // pageview1
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          actions: &lt;Widget&gt;[
            IconButton(icon: const Icon(Icons.navigate_next), onPressed: () {
              setState(() {
                if (pageViewIndex == 0) pageViewIndex = 1;
                else pageViewIndex = 0;
              });
            },)
          ],
        ),
        body: PageView.builder(
          itemBuilder: (context, page) {
            if (pageViewIndex == 0) return Center(child: Text(pageview0[page]));
            else return Center(child: Text(pageview1[page]));
          },
          itemCount: 3,
  
          // single controller
  //        controller: controller,
  
          // Multiple controller
          controller: (pageViewIndex == 0)? controllers[0] : controllers[1],
        ),
      );
    }
  }
  </code></pre>