带有 Navigator 的 Flutter Modal Bottom Sheet 没有按预期弹出

Flutter Modal Bottom Sheet with Navigator does not pop as expected

我正在与 ModalBottomSheet 合作,我喜欢这个包。但是,当我尝试在 ModalBottomSheet 中导航时遇到问题。

这里有一个ScreenVideo可以更好的理解

如您所见,视图显示为 ModalBottomSheet。但是弹出它时,它不是简单地从底部消失,而是弹出到带有 MaterialPage Pop 动画的空白屏幕。

我更改了我的代码,以便我可以在该 ModalBottomSheet 中使用 MaterialPageRoute-Animation 进行推送。在此之前,一切都按预期工作。

这是我的主页:

    class _AboutScreenState extends State<AboutScreen> {
  @override
  Widget build(BuildContext context) {
    return Material(
      color: AppColors.primary,
      child: Navigator(
        onGenerateRoute: (settings) => MaterialPageRoute(
          builder: (context2) => Builder(
            builder: (context) => CupertinoPageScaffold(
                backgroundColor: AppColors.secondary,
                resizeToAvoidBottomInset: false,
                child: SafeArea(
                  child: Padding(
                    padding: EdgeInsets.all(sidePadding),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            IconButtonWithExpandedTouchTarget(
                              onTapped: () {
                                Navigator.pop(context);
                              },
                              svgPath: 'assets/icons/down.svg',
                            ),
                          ],
                        ),
                        Text(
                          'About',
                          style: AppTextStyles.montserratH2SemiBold,
                        ),

                        ...

                        RoundedCornersTextButton(
                          title: 'Impressum',
                          onTap: () {
                            Navigator.pop(context);
                          },
                          textColor: AppColors.primary,
                          backgroundColor: AppColors.secondary,
                          borderColor: AppColors.primary,
                        ),
                      ],
                    ),
                  ),
                )),
          ),
        ),
      ),
    );
  }
}

我只是在关注这个example。我在这里错过了什么?使用上面的代码,我可以按预期使用 MaterialPageRoute 动画将 ModalSheet 推入,但是弹出第一个屏幕会出现问题...

如果您需要更多信息,请告诉我!感谢您的帮助:)

我认为你用错了上下文。该示例使用 rootContext 弹出,它来自层次结构中最顶层的小部件。您从 context 中弹出,定义在层次结构中最低的构建器中。

我认为您使用的上下文不正确。来自层次结构中最顶层小部件的 rootContext 是使示例流行的原因。您是从层次结构中最低的构建器弹出的,这由上下文决定。