当前位置:   article > 正文

Flutter插件开发:原生发送Event_flutterstreamhandler

flutterstreamhandler

目录

 

1、新建一个FlutterPlugin工程

2、编辑android原生工程

3、编辑ios原生工程

4、发布插件

1、新建一个FlutterPlugin工程

打开Android Studio后依次File-->New-->New Flutter Project-->Flutter Plugin,工程lib文件夹默认实现了一个FlutterPlugin,并且main.dart中有使用代码段。按照业务需求进行增加、修改。

FlutterPlugin类

  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class FlutterPlugin {
  4. StreamSubscription<dynamic> _event;
  5. initEvent() {
  6. //注册事件监听,设置event回调
  7. _event = new EventChannel("event")
  8. .receiveBroadcastStream()
  9. .listen(eventData, onError: eventError, onDone: eventDone);
  10. }
  11. /**
  12. *关闭并发送完成事件流,回调
  13. */
  14. void eventDone() {
  15. print("eventDone");
  16. }
  17. /**
  18. * 出错回调
  19. */
  20. void eventError(error) {
  21. print("eventError:$error");
  22. }
  23. /**
  24. * 接收数据回调
  25. */
  26. void eventData(event) {
  27. print("eventData:$event");
  28. }
  29. //其它代码
  30. }

main.dart类

  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_plugin/flutter_plugin.dart';
  5. void main() => runApp(MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. @override
  12. void initState() {
  13. super.initState();
  14. //注册event接收
  15. FlutterPlugin().initEvent();
  16. }
  17. //其它代码
  18. }

2、编辑android原生工程

插件工程右键找到Flutter菜单,点击“Open Android module in Android studio”在android studio中打开Android原生工程。新版本android studio默认使用kotlin语言,使用java语言请注意区别。工程打开后flutter_plugin中找到FlutterPlugin类,工程结构如下

FlutterPlugin类

  1. package plugin.flutter.zhou.com.flutter_plugin
  2. import io.flutter.plugin.common.EventChannel
  3. import io.flutter.plugin.common.EventChannel.EventSink
  4. import io.flutter.plugin.common.EventChannel.StreamHandler
  5. import io.flutter.plugin.common.MethodCall
  6. import io.flutter.plugin.common.MethodChannel
  7. import io.flutter.plugin.common.MethodChannel.MethodCallHandler
  8. import io.flutter.plugin.common.MethodChannel.Result
  9. import io.flutter.plugin.common.PluginRegistry.Registrar
  10. class FlutterPlugin : MethodCallHandler {
  11. //*****************flutter调用原生******************************
  12. companion object {
  13. @JvmStatic
  14. fun registerWith(registrar: Registrar) {
  15. //实例化插件对象
  16. val plugin = FlutterPlugin()
  17. //调用方法注册event handler
  18. plugin.register(registrar)
  19. }
  20. }
  21. private var eventSink: EventSink? = null;
  22. private val streamHandler = object : StreamHandler {
  23. override fun onListen(obj: Any?, sink: EventSink?) {
  24. eventSink = sink
  25. }
  26. override fun onCancel(obj: Any?) {
  27. //取消时候清空sink
  28. eventSink = null
  29. }
  30. }
  31. /**
  32. * 注册event Handler
  33. */
  34. fun register(registrar: Registrar) {
  35. //event字符串为自定义名称flutter、android、ios三端一致即可
  36. val eventChannel = EventChannel(registrar.messenger(), "event")
  37. //设置handler
  38. eventChannel.setStreamHandler(streamHandler)
  39. //测试event发送
  40. sendEvent("Send event data")
  41. }
  42. /**
  43. * 发送event,注意数据类型三端一致
  44. */
  45. fun sendEvent(event: Any) {
  46. eventSink?.success(event)
  47. }
  48. /*
  49. *处理method
  50. */
  51. override fun onMethodCall(call: MethodCall, result: Result) {
  52. //todo
  53. }
  54. }

3、编辑ios原生工程

插件工程右键找到Flutter菜单,点击“Open IOS module in Xcode”在xcode中打开IOS原生工程。自动创建的工程没有设置Vsersion和Build,编译请注意相关错误。工程结构如下图

SwiftFlutterPlugin类

  1. import Flutter
  2. import UIKit
  3. public class SwiftFlutterPlugin: NSObject,Flutter.FlutterPlugin {
  4. let instance = SwiftFlutterPlugin()
  5. //注册event
  6. instance.registrarEvent(registrar: registrar)
  7. }
  8. //EventStreamHandler为自定义类
  9. private let eventStreamHandler = EventStreamHandler()
  10. func registrarEvent(registrar: FlutterPluginRegistrar) {
  11. //event字符串为自定义名称flutter、android、ios三端一致即可
  12. let eventChannel = FlutterEventChannel.init(name: "event", binaryMessenger:registrar.messenger())
  13. //设置handler,初学swift,是不是有其它写法?
  14. eventChannel.setStreamHandler((eventStreamHandler as! FlutterStreamHandler & NSObjectProtocol))
  15. }
  16. //处理method,原始工程没有if-else,笔者增加
  17. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  18. //todo
  19. }
  20. }

EventStreamHandler类

  1. //
  2. // EventStreamHandler.swift
  3. // flutter_plugin
  4. //
  5. //
  6. import Foundation
  7. class EventStreamHandler: FlutterStreamHandler {
  8. private var eventSink:FlutterEventSink? = nil
  9. func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  10. eventSink = events
  11. return nil
  12. }
  13. func onCancel(withArguments arguments: Any?) -> FlutterError? {
  14. eventSink = nil
  15. return nil
  16. }
  17. //发送event
  18. public func sendEvent(event:Any) {
  19. eventSink?(event)
  20. }
  21. }

4、发布插件

请参考社区教程

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/383242
推荐阅读
相关标签
  

闽ICP备14008679号