Flutter与iOS原生混合开发 iOS项目集成Flutter

1.创建flutter module
进入iOS工程根目录的上一级,创建flutter module工程

flutter create --template module fluttermodule

2.进入iOS工程根目录,编辑podfile文件

flutter_application_path = '../fluttermodule'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target "iOSDemo" do

use_frameworks!
install_all_flutter_pods(flutter_application_path)

end

post_install do |installer|
  flutter_post_install(installer) if defined?(flutter_post_install)
end

执行pod install
3.iOS调用Flutter
在AppDelegate注册FlutterEngine

import UIKit
import Flutter
import FlutterPluginRegistrant

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        GeneratedPluginRegistrant.register(with: self.flutterEngine);
        flutterEngine.run()
        return true
    }
}

使用 FlutterEngine调用utterViewController

import UIKit
import Flutter

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    // Make a button to call the showFlutter function when pressed.
    let button = UIButton(type:UIButton.ButtonType.custom)
    button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
    button.setTitle("Show Flutter!", for: UIControl.State.normal)
    button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
    button.backgroundColor = UIColor.blue
    self.view.addSubview(button)
  }

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

参考链接
https://www.jianshu.com/p/aac1f387f3c0
https://www.jianshu.com/p/c3e5e210a585

相关推荐

  1. FlutteriOS原生混合开发 iOS项目集成Flutter

    2024-06-07 13:38:01       36 阅读
  2. iOS 集成 Flutter Module

    2024-06-07 13:38:01       57 阅读
  3. Flutter 项目添加 IOS 小组件开发记录

    2024-06-07 13:38:01       37 阅读
  4. FlutteriOS和Android原生页面交互

    2024-06-07 13:38:01       36 阅读
  5. iOS 开发者的 Flutter 入门课

    2024-06-07 13:38:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 13:38:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 13:38:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 13:38:01       87 阅读
  4. Python语言-面向对象

    2024-06-07 13:38:01       96 阅读

热门阅读

  1. KNN算法实例_电影类型判断

    2024-06-07 13:38:01       31 阅读
  2. C++中为什么尽量使用using 代替 typedef

    2024-06-07 13:38:01       29 阅读
  3. Vue 组件之间的通信

    2024-06-07 13:38:01       34 阅读
  4. 连续分配存储管理方式

    2024-06-07 13:38:01       21 阅读
  5. C++实现图像的模拟运动模糊

    2024-06-07 13:38:01       23 阅读
  6. 1103. 分糖果 II

    2024-06-07 13:38:01       27 阅读
  7. 力扣每日一题 6/7

    2024-06-07 13:38:01       35 阅读
  8. input 输入框只能输入数字的处理方式

    2024-06-07 13:38:01       26 阅读
  9. shujugeshi

    2024-06-07 13:38:01       23 阅读
  10. linux系统使用达梦数据库

    2024-06-07 13:38:01       27 阅读
  11. Stream流

    Stream流

    2024-06-07 13:38:01      18 阅读