嵌套PageView:当内部PageView到达末尾时改变外部PageView

Rém*_*let 5 flutter

考虑一个PageView用法,其中我们有一个PageView在另一个里面:

PageView(
  children: [
    Container(color: Colors.red),
    Column(
      children: [
        Text('Title'),
        PageView(
          children: [
            Container(color: Colors.green),
            Container(color: Colors.yellow),
          ],
        ),
      ],
    )
  ],
);
Run Code Online (Sandbox Code Playgroud)

这种架构将允许我们在外部PageView和内部PageView之间转换。但是一旦我们到达内部的PageView,我们就无法再出去了。

这是一个展示它的gif:

在此处输入图片说明

在这样的架构中,我们不能将两个PageView都“合并”为一个,因为内部的PageView不会填满屏幕

我们如何在允许从两个PageView转换而不会卡住的情况下获得类似的结果?

Ham*_*edi 0

一种简单的方法是使用通知侦听器和 animateToPage:

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Container(
      color: Colors.yellow,
      child: PageView(
        controller: _parentController,
        children: [
          Container(
            color: Colors.green,
            child: Center(
              child: Text('0'),
            ),
          ),
          NotificationListener<OverscrollNotification>(
            onNotification: (notification) {
              if (notification.overscroll < 0)
                _parentController.animateToPage(0,
                    duration: Duration(milliseconds: 400),
                    curve: Curves.easeInOutQuart);
              if (notification.overscroll > 0)
                _parentController.animateToPage(2,
                    duration: Duration(milliseconds: 400),
                    curve: Curves.easeInOutQuart);
              return false;
            },
            child: PageView(
              children: [
                Container(
                  color: Colors.green,
                  child: Center(child: Text('2: 0')),
                ),
                Container(
                  color: Colors.blue,
                  child: Center(child: Text('2: 1')),
                ),
              ],
            ),
          ),
          Container(
            color: Colors.red,
            child: Center(
              child: Text('2'),
            ),
          ),
        ],
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)