概述
需要实现native跳转到flutter 指定的路由页面。
iOS 工程中发现 FlutterViewController setInitialRouter 无效,在我的需求里面是: 在iOS现有工程集成flutter项目,然后可以跳转到任意的 业务页面。
在iOS 之中实例化页面有两种方式,如下:
1. 通过全局统一的方式进行实例子化:(无效)
swift 代码:
@objc func handleButtonAction() { let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)! flutterViewController.setInitialRoute("test1") self.navigationController?.pushViewController(flutterViewController, animated: true) }
这种方式显示效果最好,app 已启动,就会直接加载数据,是我所需要的一种,渲染效果几乎和native 一致,毫无违和感,交互非常流畅。
但是如果我想之前跳转到指定页面如:“test1” 路由页面,却发现不起作用;
2、通过全局统一pushrouter方式:(有效,效果差)
swift pushRouter:
@objc func handleButtonAction() { let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)! flutterViewController.pushRoute("test1") self.navigationController?.pushViewController(flutterViewController, animated: true) }
上面代码虽然有效,但是效果不是很好,而且有明显的push 状态。所以不是我们想要的
3、通过new FlutterViewController 方式设置(有效)
@objc func handleButtonAction2(){ let flutterViewController = FlutterViewController() flutterViewController.setInitialRoute("test1") self.navigationController?.pushViewController(flutterViewController, animated: true) }
有效,但是每次渲染都有一闪的效果,在交互上比native差一点。
4、flutter 路由代码
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter rokid', debugShowCheckedModeBanner: false,// 显示和隐藏 theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the // counter didn't reset back to zero; the application is not restarted. primarySwatch: Colors.blue, ), home: PlaygroundPage(title: '若琪实验室'), routes: <String ,WidgetBuilder>{ "test": (_) => new PlaygroundPage(title: "我是内部路由测试test00",), "test1": (_) => new PlaygroundPage(title: "我是内部路由测试test01",) }, ); } }
参考资料:
https://www.gitmemory.com/issue/flutter/flutter/29554/492593645