flutter : How to hide AlertDialog

标签 flutter dart

我有一个如下对话框:

showDialog(
    context: context,
    barrierDismissible : true,
    useRootNavigator: true,
    builder: (BuildContext context) {
      return new AlertDialog(
        shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(5.0))),
        title: new Flex(
          direction: Axis.horizontal,
          children: <Widget>[
            new CircularProgressIndicator(),
            new Padding(
              padding: EdgeInsets.only(left: 20.0),
              child: new Text("Loading...", style: new TextStyle(
                  fontSize: 14.0,
                  color: Colors.black54,
                  fontFamily: "GoogleSans",
                  fontWeight: FontWeight.w400
              ), textAlign: TextAlign.left),
            )
          ],
        ),
      );
    },
  );

并尝试使用函数关闭它:

Navigator.of(context, rootNavigator: true).pop();

在开发过程中,导航器功能运行良好并按预期关闭对话框。但是当我在 Google Play 商店中上传应用程序并通过 Google Play 商店运行应用程序时,导航器功能实际上会关闭应用程序而不是关闭对话框。为什么会发生这样的事?我的代码有问题吗?

这是我的flutter doctor -v

Flutter (Channel stable, v1.12.13+hotfix.7, on Mac OS X 10.15.3 19D76, locale en-ID)
    • Flutter version 1.12.13+hotfix.7 at /Users/macpro/Documents/Development/flutter
    • Framework revision 9f5ff2306b (2 weeks ago), 2020-01-26 22:38:26 -0800
    • Engine revision a67792536c
    • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/macpro/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        sudo gem install cocoapods

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 43.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.42.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.8.0

[✓] Connected device (1 available)
    • SM J330G • 4200db84cc425415 • android-arm • Android 9 (API 28)

最佳答案

不要使用 Navigator.of(context, rootNavigator: true).pop();,而是使用 Navigator.of(context).pop();

这应该可以解决问题。仅当您的应用程序中有多个 Navigator 对象时,才需要使用 Navigator.of(context, rootNavigator: true).pop();

否则,不需要声明rootNavigator: True

关于 flutter : How to hide AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60181566/

相关文章:

flutter - 如何调整文件大小然后转换为base64?

dart - flutter : Where is the title of Material App used?

flutter - 在flutter中,如何在ListView的底部和顶部添加按钮

dart - 如何将 FutureBuilder<File> 转换为 BoxDecoraiton Image

flutter - 如何在 map 中的两个标记之间进行缩放

dart - 全局脚手架在 flutter 中显示 Snackbar

flutter - 使用与通过.env文件相同的方法从build命令获取flv中的env环境

dart - 如何在上传之前检查视频的文件大小

flutter - 更改图标颜色或删除它

Flutter - 如何在 PageView Widget 中的页面之间传递数据?