一 创建flutter 模块
命令行
flutter create --template module my_flutter
创建完成后,该模块和普通的flutter项目一直,可以通过android studio或vscode打开、开发、运行;
- 和之前项目不同的ios和android项目是一个隐藏文件,并且我们通常不会单独打开它们再来运行;
- 它们的作用是将flutter module进行编译,之后继承到现有的项目中
my_flutter/ ├── .ios/ ├── .android/ ├── lib/ │ └── main.dart ├── test/ └── pubspec.yaml
二 嵌入到ios 项目
主要是通过pod 进行设置,之后pod install
注意my_flutter 的路径对不对
platform :ios, '12.0' # 添加模块所在路径 flutter_application_path = '../my_flutter' load file.join(flutter_application_path, '.ios', 'flutter', 'podhelper.rb') target 'flutterhybriddemo' do # comment the next line if you don't want to use dynamic frameworks use_frameworks! # 安装flutter模块 install_all_flutter_pods(flutter_application_path) # pods for flutterhybriddemo end post_install do |installer| flutter_post_install(installer) if defined?(flutter_post_install) end
三 ios 项目中调用
为了在既有的ios应用中展示flutter页面,需要启动 flutter engine和 flutterviewcontroller
appdelegate 中设置代码
import uikit import flutterpluginregistrant @main class appdelegate: uiresponder, uiapplicationdelegate { // 1.创建一个flutterengine对象 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. // 2 启动 flutterengine.run() return true }
在viewcontrolelr 设置的代码
import uikit import flutter class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // do any additional setup after loading the view. let btn = uibutton(type: uibutton.buttontype.custom) btn.settitle("加载flutter", for: uicontrol.state.normal) btn.frame = cgrect(x: 50, y: 50, width: 200, height: 50) btn.backgroundcolor = uicolor.blue btn.addtarget(self, action: #selector(showflutter), for: uicontrol.event.touchupinside) view.addsubview(btn) } @objc func showflutter(){ let flutterengine = (uiapplication.shared.delegate as! appdelegate).flutterengine let fluttercontroller = flutterviewcontroller(engine:flutterengine, nibname: nil, bundle: nil) present(fluttercontroller, animated: true) } }
显示的结果 顺利加载出flutter 的页面
到此这篇关于ios 项目嵌入flutter 运行的文章就介绍到这了,更多相关ios 项目嵌入flutter内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论