flutter-plugins by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-plugins搭建并配置 Flutter 插件包,处理标准方法通道、FFI 集成和联合插件架构。它配置平台特定的原生代码环境,实现 Android v2 嵌入生命周期接口,并建立平台接口包。
使用以下决策树来确定插件架构和模板:
dart:ffi 使用 C/C++ 原生代码?
--template=plugin_ffi。
dart:ffi 和方法通道?
--template=plugin(非 FFI)。您必须在标准插件结构内手动配置 FFI。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
收集插件需求 停止并询问用户:
com.example)?android,ios,web,linux,macos,windows)?生成插件包 根据用户的参数执行 Flutter CLI 命令。
标准插件示例:
flutter create --org com.example --template=plugin --platforms=android,ios,macos -a kotlin -i swift my_plugin
FFI 插件示例:
flutter create --template=plugin_ffi my_ffi_plugin
3. 配置联合插件架构(如果适用) 如果用户请求联合插件,配置面向应用的包的 pubspec.yaml 以认可平台实现。
# 面向应用的 pubspec.yaml
flutter:
plugin:
platforms:
android:
default_package: my_plugin_android
windows:
default_package: my_plugin_windows
dependencies:
my_plugin_android: ^1.0.0
my_plugin_windows: ^1.0.0
对于平台实现包,定义 implements 键:
# 平台实现 pubspec.yaml(例如,my_plugin_windows)
flutter:
plugin:
implements: my_plugin
platforms:
windows:
pluginClass: MyPlugin
4. 准备用于编辑的原生环境 在修改原生代码之前,您 必须 构建示例应用以解析依赖项并生成必要的文件。
cd my_plugin/example
flutter build apk --config-only # 对于 Android
flutter build ios --no-codesign --config-only # 对于 iOS
flutter build windows # 对于 Windows
5. 实现 Android v2 嵌入生命周期 修改 Android 插件类(例如 android/src/main/kotlin/com/example/my_plugin/MyPlugin.kt)。将逻辑从 registerWith() 提取到一个与 onAttachedToEngine() 共享的私有方法中。如果需要上下文,实现 ActivityAware 或 ServiceAware。
package com.example.my_plugin
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
class MyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
setupChannel(flutterPluginBinding.binaryMessenger)
}
// 用于 v1 和 v2 嵌入兼容性的共享私有方法
private fun setupChannel(messenger: BinaryMessenger) {
channel = MethodChannel(messenger, "my_plugin")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
// 处理 Activity 附加
}
override fun onDetachedFromActivityForConfigChanges() {}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}
override fun onDetachedFromActivity() {}
}
6. 验证和修复 运行插件测试和分析器以确保生成的代码有效。
cd my_plugin
flutter analyze
flutter test
如果分析器报告缺少依赖项或未解析的原生符号,请验证步骤 4(构建示例应用)是否成功执行。修复原生代码块中任何缺失的导入。
--template=plugin_ffi 创建的包内使用方法通道。如果两者都需要,请使用 --template=plugin。build.gradle)、iOS(.xcworkspace)或 Windows(.sln)文件之前,至少构建一次示例项目(flutter build <platform>)。lib/<package_name>.dart)中留下未记录的公共成员。FlutterPlugin)。不要仅依赖已弃用的 PluginRegistry.Registrar。.android 或 .ios 目录;只编辑插件 android/ 或 ios/ 目录内的原生代码。每周安装量
944
代码仓库
GitHub 星标数
784
首次出现
2026年3月4日
安全审计
已安装于
codex911
opencode908
cursor908
github-copilot907
gemini-cli906
kimi-cli905
Scaffolds and configures Flutter plugin packages, handling standard method channels, FFI integrations, and federated plugin architectures. It configures platform-specific native code environments, implements Android v2 embedding lifecycle interfaces, and establishes platform interface packages.
Use the following decision tree to determine the plugin architecture and template:
dart:ffi?
--template=plugin_ffi.
dart:ffi and Method Channels?
--template=plugin (Non-FFI). You must configure FFI manually within the standard plugin structure.Gather Plugin Requirements STOP AND ASK THE USER:
com.example)?android,ios,web,linux,macos,windows)?Generate the Plugin Package Execute the Flutter CLI command based on the user's parameters.
Standard Plugin Example:
flutter create --org com.example --template=plugin --platforms=android,ios,macos -a kotlin -i swift my_plugin
FFI Plugin Example:
flutter create --template=plugin_ffi my_ffi_plugin
3. Configure Federated Plugin Architecture (If Applicable) If the user requested a federated plugin, configure the pubspec.yaml of the app-facing package to endorse the platform implementations.
# App-facing pubspec.yaml
flutter:
plugin:
platforms:
android:
default_package: my_plugin_android
windows:
default_package: my_plugin_windows
dependencies:
my_plugin_android: ^1.0.0
my_plugin_windows: ^1.0.0
For the platform implementation packages, define the implements key:
# Platform implementation pubspec.yaml (e.g., my_plugin_windows)
flutter:
plugin:
implements: my_plugin
platforms:
windows:
pluginClass: MyPlugin
4. Prepare Native Environments for Editing Before modifying native code, you MUST build the example app to resolve dependencies and generate necessary files.
cd my_plugin/example
flutter build apk --config-only # For Android
flutter build ios --no-codesign --config-only # For iOS
flutter build windows # For Windows
5. Implement Android v2 Embedding Lifecycle Modify the Android plugin class (e.g., android/src/main/kotlin/com/example/my_plugin/MyPlugin.kt). Extract logic from registerWith() into a private method shared with onAttachedToEngine(). Implement ActivityAware or ServiceAware if context is needed.
package com.example.my_plugin
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
class MyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var channel : MethodChannel
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
setupChannel(flutterPluginBinding.binaryMessenger)
}
// Shared private method for v1 and v2 embedding compatibility
private fun setupChannel(messenger: BinaryMessenger) {
channel = MethodChannel(messenger, "my_plugin")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
// Handle Activity attachment
}
override fun onDetachedFromActivityForConfigChanges() {}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}
override fun onDetachedFromActivity() {}
}
6. Validate and Fix Run the plugin tests and analyzer to ensure the generated code is valid.
cd my_plugin
flutter analyze
flutter test
If the analyzer reports missing dependencies or unresolved native symbols, verify that step 4 (building the example app) was executed successfully. Fix any missing imports in the native code blocks.
--template=plugin_ffi. If both are required, use --template=plugin.flutter build <platform>) at least once before attempting to edit or analyze native Android (build.gradle), iOS (.xcworkspace), or Windows (.sln) files.lib/<package_name>.dart).FlutterPlugin). Do not rely solely on the deprecated PluginRegistry.Registrar.Weekly Installs
944
Repository
GitHub Stars
784
First Seen
Mar 4, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex911
opencode908
cursor908
github-copilot907
gemini-cli906
kimi-cli905
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
YouTube视频分析师 - 逆向分析病毒内容公式,提取钩子、留存机制与情感触发点
647 周安装
SQLiteData 使用指南:SwiftData 轻量级替代方案,支持 CloudKit 同步
CTF密码学挑战速查指南 | 经典/现代密码攻击、RSA/ECC/流密码实战技巧
648 周安装
Bitrefill CLI:让AI智能体自主购买数字商品,支持加密货币支付
Bilibili 字幕提取工具 - 支持 AI 字幕检测与 ASR 转录,一键下载视频字幕
648 周安装
assistant-ui thread-list 线程列表:管理多聊天线程的 React AI SDK 组件
649 周安装
.android or .ios directories inside a Flutter module; only edit the native code inside the plugin's android/ or ios/ directories.