Flutter学习笔记(三)混合开发

987 阅读4分钟

Flutter设计初衷并不是为了和其它平台进行混合开发,它的目的是为了打造一个完整的跨平台应用程序。但是,在实际开发中,原有项目完全使用Flutter进行重构并不现实,对于原有项目我们更多可能采用混合开发的方式。
Flutter 可以作为 frameworks 添加到 iOS 项目,iOS项目引入Flutter module。

1.创建 Flutter module

通过命令创建 Flutter module,打开终端,输入如下:

cd ios 项目根目录
flutter create --template module my_flutter

image

执行完毕后,Flutter module将会创建在 ios项目/my_flutter目录下,目录结构如图:

image

.ios 是隐藏目录,可以单独运行Flutter module,测试此模块的功能,iOS代码添加到现有应用程序的项目或插件中,而不是添加到模块的.ios /目录中。

由于.ios /目录是自动生成的,因此请勿对其进行源代码控制。在新机器上构建模块之前,请先在my_flutter目录中运行flutter pub get来重新生成.ios /目录,然后再使用Flutter模块构建iOS项目。

2.模块嵌入到现有应用程序中

将Flutter模块嵌入到现有iOS应用程序中有两种方式:

  • 2.1.使用CocoaPods和已安装的Flutter SDK(推荐)。
  • 2.2.为Flutter引擎,已编译的Dart代码和所有Flutter插件创建 frameworks。手动嵌入 frameworks,并在Xcode中更新现有应用程序的构建设置。

2.1 使用CocoaPods和已安装的Flutter SDK

此方法需要所有的相关开发的人员安装 Flutter 环境。

修改iOS应用程序中 Podfile 文件,如果没有则手动创建,内容如下:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

# 添加模块所在路径
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')


target 'My App' do
  
  use_frameworks!

  # 安装Flutter模块
  install_all_flutter_pods(flutter_application_path)

end

执行 pod install 命令:

image

当在my_flutter / pubspec.yaml中更改Flutter插件的依赖性或者第一次运行时,请在Flutter模块目录中运行flutter pub get来刷新podhelper.rb脚本读取的插件列表。然后,从应用程序目录再次运行pod install。

podhelper.rb脚本将插件Flutter.framework和App.framework嵌入到项目中。

用 Xcode 打开 My App.xcworkspace,如果已经打开则需要关闭重新打开,使用 ⌘B 编译项目,编译成功。

2.2.在Xcode中嵌入 Flutter Frameworks

通过命令生成必要的 Frameworks,并通过手动编辑现有的Xcode项目将它们嵌入到应用程序中。如果团队成员无法在本地安装Flutter SDK和CocoaPods,或者您不想在现有应用程序中将CocoaPods用作依赖项管理器,则可以使用此方式。每次在Flutter模块中进行代码更改时,都必须运行 flutter build ios 。

运行如下命令生成 Frameworks:

flutter build ios-framework --output=./Flutter/

image

执行完毕后在对应的目录下生成相关编译产物:

image

frameworks 已经生成,将 frameworks 链接到 iOS 应用程序有很多中方法,下面介绍一种, 打开 Xcode,

将 App.framework 和 Flutter.framework 拖入Build Settings > Build Phases > Link Binary With Libraries: image

此时在项目的左侧增加 Frameworks 目录:

image

在 Build Settings -> Search Paths -> Framework Search Paths 中添加 ${PODS_ROOT}/../my_flutter/Flutter/Release

image

使用 ⌘B 编译项目,编译成功。

3.创建 FlutterEngine 和 FlutterViewController

将 Flutter 页面嵌入 iOS 应用程序需要创建 FlutterEngine(Flutter 引擎) 和 FlutterViewController,FlutterEngine 是Dart VM和Flutter运行时的 host,FlutterViewController 附着于 FlutterEngine,作用是通信和显示 Flutter UI。

创建 FlutterEngine:


import UIKit
import Flutter

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
  // 创建Flutter引擎
  lazy var flutterEngine = FlutterEngine(name: "my flutter engine")

  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // 2.启动flutter引擎
    flutterEngine.run();

    return super.application(application, didFinishLaunchingWithOptions: launchOptions);
  }
}

添加一个按钮,跳转到 Flutter 页面:

import UIKit
import Flutter

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = CGRect(x: 80, y: 100, width: 120, height: 45)
        button.addTarget(self, action: #selector(showFlutter), for: UIControl.Event.touchUpInside)
        button.setTitle("显示 Flutter", for: UIControl.State.normal)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)
        
    }
    
    @objc func showFlutter(){
        let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
        let flutterVC = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
        flutterVC.modalPresentationStyle = .fullScreen
        present(flutterVC, animated: true, completion: nil)
        
        
    }

}

上面的代码使用了缓存 FlutterEngine,当然这也是推荐的一种方式。

还有一种方式是是使用隐含的FlutterEngine,使用隐含的FlutterEngine会明显增加显示Flutter UI的时间,通常不建议这样做,如果很少显示 Flutter 屏幕,没有好的方法来确定何时启动Dart VM以及何时Flutter不需要在视图控制器之间保持状态,则这可能很有用。

func showFlutter() {
  let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil)
  present(flutterViewController, animated: true, completion: nil)
}

4. Flutter模块调试

一旦将Flutter模块继承到你的项目中,并且使用Flutter平台的API运行Flutter引擎或UI,那么就可以先普通的Android或者iOS一样来构建自己的Android或者iOS项目了

但是Flutter的有一个非常大的优势是其快速开发,也就是hot reload。

那么对应Flutter模块,我们如何使用hot reload加速我们的调试速度呢?

  • 在Flutter项目中,使用flutter attach
--app-id是指定哪一个应用程序# -d是指定连接哪一个设备
flutter attach --app-id com.coderwhy.ios-my-test -d 3D7A877C-B0DD-4871-8D6E-0C5263B986CD

截屏2020-11-29 下午12.45.04.png

然后就可以使用(r / R)来及时刷新了。