flutter_module

  • 使用Flutter模块在原生Android (Kotlin) 和 iOS (Swift) 系统之间传递参数的示例。

结果

配置Android项目 (Kotlin)

  • 创建Flutter模块
flutter create -t module --org br.com.megamil flutter_module
  • 修改build.gradle (app),在“buildTypes”中添加以下代码段

buildTypes {
    ...
    profile {
        initWith debug
    }
}
  • 在同一个build.gradle (app) 中,在“dependencies”中添加以下代码段

dependencies {
    ...
    debugImplementation 'br.com.megamil.flutter_module:flutter_debug:1.0'
    profileImplementation 'br.com.megamil.flutter_module:flutter_profile:1.0'
    releaseImplementation 'br.com.megamil.flutter_module:flutter_release:1.0'
}
  • 修改settings.gradle,添加maven仓库,如以下示例所示,“repo”文件夹的地址应指向导出的库所在的位置

dependencyResolutionManagement {
    String storageUrl = System.env.FLUTTER_STORAGE_BASE_URL ?: "https://storage.googleapis.com"
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            url '../../build/host/outputs/repo'
        }
        maven {
            url "$storageUrl/download.flutter.io"
        }
    }
}
  • 导出到Android
flutter build aar

准备调用流程

  • 在AndroidManifest.xml中添加此activity

<activity
    android:name="io.flutter.embedding.android.FlutterActivity"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize" />

配置iOS项目 (Swift)

  • 配置Podfile。将“flutter_application_path”更新为到Flutter中的“.ios”文件夹的路径。在这里,它是这样的:flutter_module/.ios,而使用模块的iOS在这个文件夹内,但位于另一个子文件夹中:flutter_module/examples/ios/UsingFlutterModule,因此需要向上返回3个级别“../../../”。

flutter_application_path = '../../../'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

另外,添加

install_all_flutter_pods(flutter_application_path)

以及代码段

post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end

完整示例

flutter_application_path = '../../../'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'UsingFlutterModule' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  install_all_flutter_pods(flutter_application_path)
  # Pods for UsingFlutterModule

end

post_install do |installer|
  flutter_post_install(installer) if defined?(flutter_post_install)
end
  • 导出到iOS
flutter build ios-framework

GitHub

查看 Github