带导航器的 Flutter Modal Bottom Sheet 没有按预期弹出

标签 flutter dart navigator flutter-showmodalbottomsheet materialpageroute

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

这是一个ScreenVideo以便更好地理解。

如您所见,View 显示为 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 弹出,定义在层次结构中最低的构建器中。

关于带导航器的 Flutter Modal Bottom Sheet 没有按预期弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70356309/

相关文章:

android - GetCurrentLocation 在 Android 的调试中工作但在发布 apk 中不起作用

dart - 水合 block 不持久

dart - 如何从文件中获取字节数据

flutter - 如何更改位于自定义底部应用栏中心的 FAB 周围的颜色?

android - Navigator.push 在 ListView 中,ListTile 不起作用

flutter - Dart/Flutter 中 Money 的数据类型

android - Flutter - 无法发出 POST 请求

flutter - 在 Flutter 中使用 onPressed 调用函数时出现问题

javascript - 学习 JavaScript - for-in 语句最多。结果

javascript - 当用户在 Android 上取消 native 选择器时,navigator.share 不会解析或拒绝