• flutter 与原生 (iOS-swift)


    一.创建关联module

    1.创建原生项目iOS_demo,找到者自己的iOS工程目录

    2.创建flutter_module(2种方式)

    2.1.终端命令 cd 到原生项目iOS_demo同级目录下 创建flutter模块如flutter_native

    flutter create --template module flutter模块名

    2.2.AndroidStudio 创建Modlue

     

    此时文件生成flutter_native文件夹 

    3.flutter_module的pubspec.yaml下添加自己的依赖库,终端执行flutter pub get拉取依赖库

    flutter pub get

    4.原生iOS项目下pod init 创建Podfile 文件,Podfile添加flutter_module

    1. # Uncomment the next line to define a global platform for your project
    2. # platform :ios, '9.0'
    3. # 添加模块所在路径
    4. flutter_application_path = '../flutter_native'
    5. load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    6. target 'flutter_demo' do
    7. # Comment the next line if you don't want to use dynamic frameworks
    8. use_frameworks!
    9. # 安装Flutter模块
    10. install_all_flutter_pods(flutter_application_path)
    11. end

    5.pod install 安装依赖

    二. 代码

    在iOS是原生项目AppDelegate导入flutter库

    1. import Flutter
    2. import FlutterPluginRegistrant

    创建全局的引擎入口

    1. // 1. 创建 flutterEngine name: 引擎名称
    2. lazy var flutterEngine = FlutterEngine(name: "Evan_Engine")
    3. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    4. // 2.启动引擎
    5. flutterEngine.run()
    6. return true
    7. }

    在页面中进行跳转flutter

    1. class ViewController: UIViewController {
    2. override func viewDidLoad() {
    3. super.viewDidLoad()
    4. view.backgroundColor = UIColor.brown
    5. // Do any additional setup after loading the view.
    6. }
    7. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    8. //搜索到了很多中获取控制器跳转的方法,感觉这种获取 控制器跳转最为流畅 (自我感觉)
    9. let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
    10. let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
    11. present(flutterViewController, animated: true, completion: nil)
    12. }
    13. }

    三.flutter热加载编写代码

    此时flutter   module里面AndroidStudio 点击下图标,或者vscode终端输入 flutter attach

    flutter attach

     后xcode 跑模拟器(1.注意与flutter attach是同一设备,设备处于激活状态 2.APP退出)

    修改后注意保存是不能实时更新UI,需要终端输入R进行热更新

  • 相关阅读:
    一文秒懂AGC/AVC,以及什么是光伏电站AGC,AVC装置?AGC,AVC装置的功能与用途?
    distcc分布式编译
    new bing 初体验:辅助看论文刚刚好
    Spring Security(6)
    RISC-V 编译环境搭建:riscv-gnu-toolchain 和 riscv-tools
    沃尔玛、美客多跨境平台自养号全攻略:防关联环境系统搭建与养号技巧
    k8s之容器内存与JVM内存
    Variable-Length Subsequence Clustering in Time Series(TKDE)
    FPGA项目开发之AXI Stream FIFO IP
    计算机毕业设计选题推荐-springboot 网上手机销售系统
  • 原文地址:https://blog.csdn.net/yxw125125/article/details/126320627