飘动多个综合浏览量和控制器

时间:2019-09-18 03:12:06

标签: flutter pageviews

我有两次浏览量:

  • pageview0有3个页面:1、2、3
  • pageview1有3个页面:A,B,C

一次只显示一次浏览量,当我按下AppBar上的“切换”按钮时,我想在它们之间切换。

我想要什么:

如果我位于pageview0第2页,然后按切换按钮, 那么我想查看pageview1页面A。

但是下面的代码显示pageview1页面B(具有相同索引的页面)。 我也尝试使用两个PageControllers而不是一个,但是行为是相同的。 当存在多个PageView并在它们之间进行切换时,如何控制显示哪个页面?

我的代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> pageview0 = ["1", "2", "3"];
  List<String> pageview1 = ["A", "B", "C"];
  int pageViewIndex = 0;

  // Try single controller.
  PageController controller;
  // Try multiple controller.
  List<PageController> 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: <Widget>[
          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],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

根据Taufik的评论,我改用IndexedStack并运行良好。

这是新代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> pageview0 = ["1", "2", "3"];
  List<String> pageview1 = ["A", "B", "C"];
  int pageViewIndex = 0;

  // Try single controller.
  PageController controller;
  // Try multiple controller.
  List<PageController> 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: <Widget>[
          IconButton(icon: const Icon(Icons.navigate_next), onPressed: () {
            setState(() {
              if (pageViewIndex == 0) pageViewIndex = 1;
              else pageViewIndex = 0;
            });
          },)
        ],
      ),
      body: IndexedStack(
        index: pageViewIndex,
        children: <Widget> [
          PageView.builder(
            itemBuilder: (context, page) {
              return Center(child: Text(pageview0[page]));
            },
            itemCount: 3,
            controller: controllers[0],
          ),
          PageView.builder(
            itemBuilder: (context, page) {
              return Center(child: Text(pageview1[page]));
            },
            itemCount: 3,
            controller: controllers[1],
          ),
        ],
      ),
    );
  }
}