如何从父小部件访问和更改另一个小部件 pageControllers 索引?

How to access and change another widgets pageControllers index from parent widget?

我正在尝试将一个 PageView 嵌套在一个 PageView 中并访问第二个 PageView 的控制器。

父级的结构如下所示:

Scaffold(
      appBar: buildAppBar(context),
      body: PageView(
        children: [
          Column(                   //this column represents the first view of this pageview
            children: [
              Expanded(
                flex: 3,
                child: buildPreviewWidget(selection, _buildCropPreview),
              ),
              Expanded(
                flex: 2,
                 child: MediaGrid(
                         allowMultiSelection: multiSelectable,
                         collection: allCollection),
              ),
            ],
          ),
          CameraScreen() //this is the second view of the pageview and it also has a pageview with 2 children, i want to access its pageController
        ],
      ),
      bottomNavigationBar: buildBottomNavigationBar(),
    );

这是 bottomNavigationBar :

 BottomNavigationBar buildBottomNavigationBar() {
    return BottomNavigationBar(
      currentIndex: pageIndex,
      items: [
        const BottomNavigationBarItem(
          label: "Gallery",
          icon: SizedBox.shrink(),
        ),
        const BottomNavigationBarItem(
          label: "Photo",
          icon: SizedBox.shrink(),
        ),
        const BottomNavigationBarItem(
          label: "Video",
          icon: SizedBox.shrink(),
        ),
      ],
      onTap: (index) {
       //if index == 0, go to this pageViews first page
       //if index == 1, go to this pageviews second page (CameraScreen)  and to its first page
       //if index == 2, go to this pageviews second page (CameraScreen) and to its second page
      },
    );
  }

如您所见,父级 pageView 只有 2 个子级,但 bottomNavigationbar 有 3 个项目。

我已经在 bottomNavigationBar

onTap() 中以伪代码的形式编写了我想要的功能

我试过的

我试图使 CameraScreens pageControllers 索引成为必需的 属性 这样我就可以用所需的索引重建它,但这没有用。

我也试过实现通知,但也没用。可能是因为接收器必须处于活动状态?

有没有简单的方法来做到这一点?

OP 传递索引的方法是正确的方法之一,但由于它不是有状态的小部件,因此没有成功。正如评论中所讨论的,将其更改为如下内容有助于实现预期效果:

class CameraScreen extends StatefulWidget {
 final int index;
 const CameraScreen(this.index);

 @override
 _CameraScreenState createState() => _CameraScreenState ();
}

class _CameraScreenState extends State<CameraScreen > {
 @override
 Widget build(BuildContext context) {
   // here, access the index as widget.index
   return (...)
 }
}