Pigeon 是一个代码生成器工具,用于使 Flutter 和宿主平台之间的通信类型安全、更轻松、更快捷
pub地址
创建pigeons/message.dart(lib同级目录创建)
import 'package:pigeon/pigeon.dart';
// 输出配置
// 控制台执行:flutter pub run pigeon --input pigeons/message.dart
@ConfigurePigeon(PigeonOptions(
dartOut: './lib/message.dart',
kotlinOut: 'android/src/main/kotlin/com/app/xxx_plugin/Message.kt',
kotlinOptions: KotlinOptions(
// copyrightHeader: ['zero'],
package: 'com.app.xxx_plugin',
),
objcHeaderOut: 'ios/Classes/Message.h',
objcSourceOut: 'ios/Classes/Message.m',
))
@HostApi()
abstract class HostMessageApi {
String flutter2Native(String message, int type);
@async
String flutter2NativeAsync(String message, int type);
}
@FlutterApi()
abstract class FlutterMessageApi {
String native2Flutter(String message, int type);
@async
String native2FlutterAsync(String message, int type);
}
java_package 目录可以随便填写, 目录如果创建失败 就手动创建目录
flutter pub run pigeon \
--input pigeons/message.dart \
--dart_out lib/pigeon.dart \
--objc_header_out ios/Runner/pigeon.h \
--objc_source_out ios/Runner/pigeon.m \
--experimental_swift_out ios/Runner/Pigeon.swift \
--java_out ./android/app/src/main/java/io/flutter/plugins/Pigeon.java \
--java_package "io.flutter.plugins"
android/app/src/main/java/io/flutter/plugins/Pigeon.java
ios/Runner/pigeon.h
package com.app.xxx_plugin
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
/** XxxPlugin */
class XxxPlugin : FlutterPlugin, HostMessageApi {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var nativeMessage: FlutterMessageApi
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
HostMessageApi.setUp(flutterPluginBinding.binaryMessenger, this)
nativeMessage = FlutterMessageApi(flutterPluginBinding.binaryMessenger)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}
override fun flutter2Native(message: String, type: Long): String {
print("flutter2Native=message=$message=type=$type")
return "$message"
}
override fun flutter2NativeAsync(
message: String,
type: Long,
callback: (Result<String>) -> Unit
) {
print("flutter2NativeAsync=message=$message=type=$type")
callback(Result.success("flutter2NativeAsync=message=$message=type=$type"));
}
}
import 'package:xxx_plugin/message.dart';
class XxxPlugin extends FlutterMessageApi {
static final HostMessageApi _flutterMessage = HostMessageApi();
factory XxxPlugin() => _getInstance();
static XxxPlugin get instance => _getInstance();
static XxxPlugin ? _instance;
static XxxPlugin _getInstance() {
_instance ??= XxxPlugin._internal();
FlutterMessageApi.setup(_instance);
return _instance!;
}
XxxPlugin._internal();
///flutter发送消息给原生
Future<String> flutter2Native(String message,int type) {
return _flutterMessage.flutter2Native(message, type);
}
Future<String> flutter2NativeAsync(String message,int type) {
return _flutterMessage.flutter2NativeAsync(message, type);
}
@override
String native2Flutter(String message, int type) {
print('message=$message = type=$type');
return 'native2Flutter=message=$message = type=$type';
}
@override
Future<String> native2FlutterAsync(String message, int type) {
print('message=$message = type=$type');
return Future(() => 'native2Flutter=message=$message = type=$type');
}
}
await HostMessageApi().flutter2Native('111111', 2);
#import "Message.h"
import Flutter
import UIKit
public class XxxPlugin: NSObject, FlutterPlugin,HostMessageApi {
public func flutter2NativeMessage(_ message: String, type: NSNumber, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) -> String? {
print("ios->flutter2NativeMessage===="+message);
return "ios->flutter2Native=message=\(message)=type=\(type)"
}
public func flutter2NativeAsyncMessage(_ message: String, type: NSNumber, completion: @escaping (String?, FlutterError?) -> Void) {
print("ios->flutter2NativeAsyncMessage===2");
completion(message,nil)
}
public var nativeMessage:FlutterMessageApi!
public static func register(with registrar: FlutterPluginRegistrar) {
let messenger: FlutterBinaryMessenger = registrar.messenger()
let api = XxxPlugin.init()
HostMessageApiSetup(messenger, api)
api.nativeMessage = FlutterMessageApi(binaryMessenger: messenger)
}
}