赞
踩
目录
打开Android Studio后依次File-->New-->New Flutter Project-->Flutter Plugin,工程lib文件夹默认实现了一个FlutterPlugin,并且main.dart中有使用代码段。按照业务需求进行增加、修改。
FlutterPlugin类
- import 'dart:async';
-
- import 'package:flutter/services.dart';
-
- class FlutterPlugin {
-
- StreamSubscription<dynamic> _event;
-
- initEvent() {
- //注册事件监听,设置event回调
- _event = new EventChannel("event")
- .receiveBroadcastStream()
- .listen(eventData, onError: eventError, onDone: eventDone);
- }
-
- /**
- *关闭并发送完成事件流,回调
- */
- void eventDone() {
- print("eventDone");
- }
-
- /**
- * 出错回调
- */
- void eventError(error) {
- print("eventError:$error");
- }
-
- /**
- * 接收数据回调
- */
- void eventData(event) {
- print("eventData:$event");
- }
-
-
- //其它代码
- }
main.dart类
- import 'package:flutter/material.dart';
- import 'dart:async';
-
- import 'package:flutter/services.dart';
- import 'package:flutter_plugin/flutter_plugin.dart';
-
- void main() => runApp(MyApp());
-
- class MyApp extends StatefulWidget {
- @override
- _MyAppState createState() => _MyAppState();
- }
-
- class _MyAppState extends State<MyApp> {
- @override
- void initState() {
- super.initState();
- //注册event接收
- FlutterPlugin().initEvent();
- }
- //其它代码
- }
插件工程右键找到Flutter菜单,点击“Open Android module in Android studio”在android studio中打开Android原生工程。新版本android studio默认使用kotlin语言,使用java语言请注意区别。工程打开后flutter_plugin中找到FlutterPlugin类,工程结构如下
FlutterPlugin类
- package plugin.flutter.zhou.com.flutter_plugin
-
- import io.flutter.plugin.common.EventChannel
- import io.flutter.plugin.common.EventChannel.EventSink
- import io.flutter.plugin.common.EventChannel.StreamHandler
- 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
- import io.flutter.plugin.common.PluginRegistry.Registrar
-
- class FlutterPlugin : MethodCallHandler {
-
-
- //*****************flutter调用原生******************************
- companion object {
-
- @JvmStatic
- fun registerWith(registrar: Registrar) {
- //实例化插件对象
- val plugin = FlutterPlugin()
- //调用方法注册event handler
- plugin.register(registrar)
- }
- }
-
- private var eventSink: EventSink? = null;
- private val streamHandler = object : StreamHandler {
- override fun onListen(obj: Any?, sink: EventSink?) {
- eventSink = sink
- }
-
- override fun onCancel(obj: Any?) {
- //取消时候清空sink
- eventSink = null
- }
- }
-
- /**
- * 注册event Handler
- */
- fun register(registrar: Registrar) {
- //event字符串为自定义名称flutter、android、ios三端一致即可
- val eventChannel = EventChannel(registrar.messenger(), "event")
- //设置handler
- eventChannel.setStreamHandler(streamHandler)
- //测试event发送
- sendEvent("Send event data")
- }
-
- /**
- * 发送event,注意数据类型三端一致
- */
- fun sendEvent(event: Any) {
- eventSink?.success(event)
- }
-
- /*
- *处理method
- */
- override fun onMethodCall(call: MethodCall, result: Result) {
- //todo
- }
- }
插件工程右键找到Flutter菜单,点击“Open IOS module in Xcode”在xcode中打开IOS原生工程。自动创建的工程没有设置Vsersion和Build,编译请注意相关错误。工程结构如下图
SwiftFlutterPlugin类
- import Flutter
- import UIKit
-
- public class SwiftFlutterPlugin: NSObject,Flutter.FlutterPlugin {
- let instance = SwiftFlutterPlugin()
- //注册event
- instance.registrarEvent(registrar: registrar)
- }
-
- //EventStreamHandler为自定义类
- private let eventStreamHandler = EventStreamHandler()
- func registrarEvent(registrar: FlutterPluginRegistrar) {
- //event字符串为自定义名称flutter、android、ios三端一致即可
- let eventChannel = FlutterEventChannel.init(name: "event", binaryMessenger:registrar.messenger())
- //设置handler,初学swift,是不是有其它写法?
- eventChannel.setStreamHandler((eventStreamHandler as! FlutterStreamHandler & NSObjectProtocol))
- }
- //处理method,原始工程没有if-else,笔者增加
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- //todo
- }
- }
EventStreamHandler类
- //
- // EventStreamHandler.swift
- // flutter_plugin
- //
- //
-
- import Foundation
- class EventStreamHandler: FlutterStreamHandler {
- private var eventSink:FlutterEventSink? = nil
- func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
- eventSink = events
- return nil
- }
-
- func onCancel(withArguments arguments: Any?) -> FlutterError? {
- eventSink = nil
- return nil
- }
-
- //发送event
- public func sendEvent(event:Any) {
- eventSink?(event)
- }
- }
请参考社区教程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。