0

我正在尝试将 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 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 个孩子,但底部导航栏有 3 个项目。

我已经将我想要的功能编写为伪onTap()代码bottomNavigationBar

我试过的

我试图使CameraScreens pageControllers 索引成为必需的属性,以便我可以使用所需的索引重建它,但这不起作用。

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

是否有捷径可寻?

4

1 回答 1

0

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 (...)
 }
}
于 2020-11-18T16:56:17.253 回答