首页 > 解决方案 > 从 FlutterViewController 弹回原生 iOS 应用

问题描述

我将颤振模块添加到我现有的 iOS 应用程序中。目前为了测试,我正在展示一个FlutterViewController提前FlutterEngineAppDelegate. 虽然颤动屏幕正确出现,但我无法回到原生 iOS 应用程序。

小谷歌搜索表明SystemNavigator.pop()可以完成这项工作,但它对我不起作用,正如这里这里提到的那样。我怎样才能回到我的原生 iOS 应用程序?

我显示颤动屏幕的快速侧面代码如下所示

@objc func showFlutter() {
      let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
      let flutterViewController =
          FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
        flutterViewController.modalPresentationStyle = .fullScreen
      present(flutterViewController, animated: true, completion: nil)
    }

我的 Flutter 端代码弹回原生 iOS 应用程序看起来像这样,它不起作用。

appBar: AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => SystemNavigator.pop(),
        ),
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),

如果我用它替换SystemNavigator.pop()Navigator.of(context).pop()也不起作用并显示黑屏,这是有道理的,因为它没有任何后备路线。据我了解,这并没有弹出屏幕,而是关闭了整个颤振应用程序。是不是?我该如何解决这个问题?

标签: iosswiftflutter

解决方案


exit(0)当平台是 iOS 时尝试,不知何故SystemNavigator.pop()不起作用。整个代码如下:

onPressed: () {
  if(Platform.isIOS) {
    Navigator.pop(context);
    exit(0);
  } else {
    SystemNavigator.pop(animated: true);
  }
},

推荐阅读