flutter setInitialRoute: 不生效

概述

须要实现native跳转到flutter 指定的路由页面。git

 

iOS 工程中发现 FlutterViewController  setInitialRouter 无效,在个人需求里面是: 在iOS现有工程集成flutter项目,而后能够跳转到任意的 业务页面。swift

在iOS 之中实例化页面有两种方式,以下:app

1. 经过全局统一的方式进行实例子化:(无效)

swift 代码:less

 @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 一致,毫无违和感,交互很是流畅。ide

可是若是我想以前跳转到指定页面如:“test1” 路由页面,却发现不起做用测试

 

二、经过全局统一pushrouter方式:(有效,效果差)

swift pushRouter:ui

 @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 状态。因此不是咱们想要的spa

三、经过new FlutterViewController 方式设置(有效)

 @objc func handleButtonAction2(){ let flutterViewController = FlutterViewController() flutterViewController.setInitialRoute("test1") self.navigationController?.pushViewController(flutterViewController, animated: true) }

有效,可是每次渲染都有一闪的效果,在交互上比native差一点。debug

 

四、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/492593645rest

相关文章
相关标签/搜索