• 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进行热更新

  • 相关阅读:
    猿创征文|【Typescript入门】常用数据类型(3)
    【web-music】vue3 开发遇到的问题
    torch.cuda.is_available() 解决方案
    Web安全——信息收集上篇
    【webrtc】初始mia
    基于51单片机智能热电偶温度检测PID控制恒温箱设计套件24-157
    NLP之基于Seq2Seq的单词翻译
    数据库 备份和恢复
    怎么利用互联网赚钱,网上赚钱的7种方法
    生信工作流框架搭建 | 02-nextflow 实战
  • 原文地址:https://blog.csdn.net/yxw125125/article/details/126320627