【问题标题】:Open two flutter modules from native从原生打开两个flutter模块
【发布时间】:2020-05-21 17:33:08
【问题描述】:

我正在尝试从我们的原生 iOS 应用程序中打开两个不同的颤振模块,该应用程序具有许多用 swift 编写的功能并且可以正常工作。现在我们要在Flutter 中构建两个新功能。为此,我们需要从原生 iOS 代码中打开两个不同的 Flutter 模块或方法。

我尝试了很多解决方案,但都没有运气。以下是我创建的两个示例颤振方法,它们将来会触发两个不同功能的路由:

//First Method
class MyFirstApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to My First App'),
        ),
        body: Center(
          child: Text('Hello World 1'),
        ),
      ),
    );
  }
}

//Second Method
class MySecondApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to My Second App'),
        ),
        body: Center(
          child: Text('Hello World 2'),
        ),
      ),
    );
  }
}

这是我尝试用flutter写的route:

class RouteState extends StatefulWidget {
  RouteState({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _RouteState createState() => _RouteState();
}

class _RouteState extends State<RouteState> {
  static const apiChannel = const MethodChannel('my_channel');

  Future<dynamic> handleMethod(MethodCall call) async {
    switch(call.method) {
      case 'first':
        return MyFirstApp();
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text('Hello World 0'),
      ),
    );
  }
}

现在从 iOS 开始,我尝试在原生代码库的 viewcontroller 中打开上述任何一种方法。

let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
let channel = FlutterMethodChannel(name: "my_channel", binaryMessenger: flutterViewController.binaryMessenger)
channel.invokeMethod("first", arguments: "")
//viewController.show(flutterViewController, sender: viewController)

但是什么都没有发生,没有从这里打开任何东西。也不会为任何错误打印日志。我在双方都进行了调试,断点到达了 iOS 端的所有代码,而在颤动端,一切都很好。当我只运行带有main() 的颤振模块时,它起作用了。但不是从原生打开。

【问题讨论】:

    标签: ios flutter module communication


    【解决方案1】:

    您没有在代码中调用present。如上所述here

    试试这个:

    let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
        let flutterViewController =
            FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
        present(flutterViewController, animated: true, completion: nil)
    

    【讨论】:

    • 是的,我之前做过,如果你只有一个模块,那很好,这也对我有用,但我很困惑如何处理两个不同的模块。
    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2019-02-25
    • 2021-10-12
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多