1、要将Flutter嵌入到现有应用程序中,请首先创建一个Flutter模块。
从命令行中,运行:
cd some/path/
flutter create --template module flutter_module
some/path/
├── flutter_module/
│ └── .ios/
│ └── Flutter/
│ └── podhelper.rb
└── MyApp/
└── Podfile
2、安装cocoapods
3、项目命令行下执行pod init
4、项目命令行下执行pod install
5、用wordspace重新打开项目
6、podfile文件里添加一下代码
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
然后再准备运行的target里添加
install_all_flutter_pods(flutter_application_path)
7、再次执行pod install
8、禁用bitcode
build settings里搜索bitcode,然后关闭
9、添加script
在build phases新建run script,添加以下代码
"$FLUTTER_ROOT"/packages/flutter_tools/bin/xcode_backend.sh buiObject-cld
"$FLUTTER_ROOT"/packages/flutter_tools/bin/xcode_backend.sh embed
把show environment和run script only when installing都勾上(如果原生项目出现编译错误,则把only when installing点没/点开,再次运行调试)
把Run Script移动到Dependencies后面
10、build settings 里的user-defined增加一项,配置flutter目录
FLUTTER_ROOT /Users/你的名字/Desktop/Flutter/flutter
11、注意
每次在flutter里修改pubspec.yaml时,执行完flutter packages get之后,都需要执行pod install
在flutter终端中执行,选择.ios文件里的Runner.xcworkspace里面的证书和原生项目的一致(commond+shift+.打开隐藏文件夹)
flutter clean
flutter build ios
之后执行原生程序
12、在ios上热重载
进入flutter项目命令行执行flutter attach
等命令行出现Waiting for a connection from Flutter on Android SDK built for x86...
打开flutter编写的页面,命令行会出现
Syncing files to device Android SDK built for x86...
2,707ms (!)
To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Android SDK built for x86 is available at: http://127.0.0.1:61581/06WC6YnSW2w=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
此时修改flutter代码
然后命令行输入r,就可以看到代码热重载了
命令行输入R,表示热重启
复制http://127.0.0.1:61581/06WC6YnSW2w=/在浏览器中打开,用DevTools里断点调试flutter部分