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

相關文章
相關標籤/搜索