Flutter 将flutter集成到原生项目

开发Flutter项目,有两种方式:

一种是flutter项目为主,额外添加一下原生页面,比如视频播放、直播和一些交互较复杂对性能要求较高的页面。这种比较好处理,直接创建一个Flutter项目,在需要使用原生界面的时候,通过MethodChannel或者其他通道跳转到原生界面

另外一种是:将Flutter作为子页面,以原生工程为主,部分页面跳转Flutter页面。这种需要创建Flutter Module工程,并将其集成到原生工程。

两种方式都能很好的展示Flutter界面,各有利弊,需要根据不同的项目情况去选择主次。

Flutter集成到原生的三种方式

1、Use the CocoaPods dependency manager and installed Flutter SDK. In this case, the flutter_module is compiled from the source each time the app is built. (Recommended.) (使用Cocoapods依赖管理器安装Flutter SDK,每次构建应用都会从源码中编译flutter_modul 官方推荐)

2、Create frameworks for the Flutter engine, your compiled Dart code, and all Flutter plugins. Here, you manually embed the frameworks, and update your existing application's build settings in Xcode. This can be useful for teams that don't want to require every developer to have the Flutter SDK and Cocoapods installed locally.(创建一个框架,把 Flutter 引擎、已编译的 Dart 代码和所有 Flutter 插件都放进去 这种方式你可以手动嵌入这个框架,并在 Xcode 中更改现有的应用的构建设置。如果不想要求开发团队的每一位成员都在本地安装 Flutter SDK 和 Cocoapods,这种方式比较适用。)

3、Create frameworks for your compiled Dart code, and all Flutter plugins. Use CocoaPods for the Flutter engine. With this option, embed the frameworks for your application and the plugins in Xcode, but distribute the Flutter engine as a CocoaPods podspec. This is similar to the second option, but it provides an alternative to distributing the large Flutter.xcframework.(为已编译的 Dart 代码和所有 Flutter 插件创建一个框架,对 Flutter 引擎使用 CocoaPods 来管理 这种方式是将应用内容和插件作为内嵌的框架,但将 Flutter 引擎作为 CocoaPods podspec 分发。这有点类似第二种方式,但是它为分发大型的 Flutter.xcframework 文件提供了替代方案。)

下面主要介绍第一种方式。

Flutter 集成到iOS原生项目

  1. 创建 Flutter Module 项目
    步骤比较简单,就不多介绍了,需要注意的是,在创建的时候需要先勾选使用的平台和语言,再选择是Application还是Mdoule工程。
  2. 将项目放到原生项目根目录
  3. 如果项目中没有使用Cocoapods,则创建pod 管理,具体方法自行百度
  4. 在podfile 文件中添加一下命令
ruby 复制代码
#注意这个版本号,需要和
platform :ios, '11.0'
#flutter工程路径
flutter_application_path = '../../../../xxxx_flutter'
# ruby语法,返回一个字符串,由指定的项连接在一起,并使用文件分割符进行分割
#load 引入该文件 
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'xxx' do
  # Comment the next line if you don't want to use dynamic frameworks
  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 然后打开xxx.xcworkspace 编译项目。

需要注意的是Flutter的debug和release模式和iOS的有很大区别: debug模式意味着:断点是开启的服务扩展是开启的针对快速开发和周期进行编译优化(但不是针对执行速度、二进制文件大小或者部署)调试开启,类似 开发者工具 等调试工具可以连接到进程里默认情况下flutter run是debug模式,此外debug模式在停止运行app后,是不可以直接点击打开app的。

release模式

  • 断点不可用
  • 调试信息不可见
  • 调试是禁用的
  • 编译针对快速启动、快速执行和小的package的大小进行了优化
  • 服务扩展是禁用的
arduino 复制代码
flutter run --release 可使用release进行编译运行。
flutter build --release 进行编译

问题解决

在执行pod install时可能会出现:dependency were found,but they required a higher minimum deployment target.,问题原因是由于flutter支持的最低版本和原生项目支持的最低版本不一致。解决方法:

1、直接修改原生项目中的最低支持版本调高,主要修改三个地方:

  • podfile文件中 platform :ios, '11.0'
  • Xcode 中的Target中的Minimum Deployment 最低支持版本号
  • Xcode 中的Project中的Minimum Deployment 最低支持版本号

保证这三个版本号和Flutter Module项目中:

xxx/.ios/Flutter/Flutter.podspec 中的 s.ios.deployment_target

xxx/.ios/Flutter/AppFrameworkInfo.plist 中的MinimumOSVersion

对应上。

2、按照正常逻辑来说,应该也可以修改Flutter Module中的版本号使之和原生的对应,但是不知道为啥每次修改后会被重置,目前未发现修改点,如有知道的,还请告知。

相关推荐
奇客软件21 小时前
如何从相机的记忆棒(存储卡)中恢复丢失照片
深度学习·数码相机·ios·智能手机·电脑·笔记本电脑·iphone
GEEKVIP1 天前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone
lqj_本人1 天前
flutter_鸿蒙next_Dart基础②List
flutter
lqj_本人1 天前
flutter_鸿蒙next_Dart基础①字符串
flutter
The_tuber_sadness1 天前
【Flutter】- 基础语法
flutter
一丝晨光1 天前
继承、Lambda、Objective-C和Swift
开发语言·macos·ios·objective-c·swift·继承·lambda
helloxmg1 天前
鸿蒙harmonyos next flutter通信之BasicMessageChannel获取app版本号
flutter
GEEKVIP2 天前
iPhone/iPad技巧:如何解锁锁定的 iPhone 或 iPad
windows·macos·ios·智能手机·笔记本电脑·iphone·ipad