EventChannelFlutter 端Flutter 混合开发系列 包含如下:
嵌入原生VIEw-AndroID嵌入原生VIEw-iOS与原生通信-MethodChannel与原生通信-BasicmessageChannel与原生通信-EventChannel添加 Flutter 到 AndroID Activity添加 Flutter 到 AndroID Fragment添加 Flutter 到 iOS每个工作日分享一篇,欢迎关注、点赞及转发。
Flutter 端创建 EventChannel 通道,用于与原生端通信:
var _eventChannel = EventChannel('com.Flutter.guIDe.EventChannel');
com.Flutter.guIDe.EventChannel 是 EventChannel 的名称,原生端要与之对应。
监听原生端发送的消息:
var _data; @overrIDe voID initState() { super.initState(); _eventChannel.receivebroadcastStream().Listen(_onData); } _onData(event){ setState(() { _data = event; }); }
Flutter 端完整代码:
class EventChannelDemo extends StatefulWidget { @overrIDe _EventChannelDemoState createState() => _EventChannelDemoState();}class _EventChannelDemoState extends State<EventChannelDemo> { var _eventChannel = EventChannel('com.Flutter.guIDe.EventChannel'); var _data; @overrIDe voID initState() { super.initState(); _eventChannel.receivebroadcastStream().Listen(_onData); } _onData(event){ setState(() { _data = event; }); } @overrIDe Widget build(BuildContext context) { return Scaffold( appbar: Appbar(),body: Center( child: Text('$_data'),),); }}
AndroID 端androID 下创建 EventChannelDemo:
class EventChannelDemo(var activity: Activity,messenger: BinaryMessenger):EventChannel.StreamHandler { private var channel: EventChannel private var index = 0 private var events: EventChannel.EventSink? = null init { channel = EventChannel(messenger,"com.Flutter.guIDe.EventChannel") channel.setStreamHandler(this) startTimer() } fun startTimer() { var timer = Timer().schedule(timerTask { index++ var map = mapOf("name" to "laomeng${index}","age" to "${index}" ) activity.runOnUiThread { events?.success(map) } },1000) } overrIDe fun onListen(arguments: Any?,events: EventChannel.EventSink?) { this.events = events } overrIDe fun onCancel(arguments: Any?) { this.events = null }}
events?.success(map) 方法是向 Flutter 端发送数据,此方法必须在主线程执行:
activity.runOnUiThread { events?.success(map)}
在 MainActivity 启动:
class MainActivity : FlutterActivity() { overrIDe fun configureFlutterEngine(FlutterEngine: FlutterEngine) { super.configureFlutterEngine(FlutterEngine) EventChannelDemo(this,FlutterEngine.dartExecutor.binaryMessenger) FlutterEngine.plugins.add(MyPlugin()) }}
iOS 端ios 下创建 MethodChannelDemo,按如下方式:
import Flutterimport UIKitpublic class EventChannelDemo:NSObject,FlutterStreamHandler{ var channel:FlutterEventChannel? var count = 0 var events:FlutterEventSink? public overrIDe init() { super.init() } convenIEnce init(messenger: FlutterBinaryMessenger) { self.init() channel = FlutterEventChannel(name: "com.Flutter.guIDe.EventChannel",binaryMessenger: messenger) channel?.setStreamHandler(self) startTimer() } func startTimer() { let timer = Timer.scheduledTimer(timeInterval:1,target: self,selector:#selector(self.tickDown),userInfo:nil,repeats: true) } @objc func tickDown(){ count += 1 let args = ["count":count] if(events != nil){ events!(args) } } public func onListen(withArguments arguments: Any?,eventSink events: @escaPing FlutterEventSink) -> FlutterError? { self.events = events return nil; } public func onCancel(withArguments arguments: Any?) -> FlutterError? { self.events = nil return nil; }}
在 AppDelegate 启动:
import UIKitimport Flutter@UIApplicationMain@objc class AppDelegate: FlutterAppDelegate { overrIDe func application( _ application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { let controller : FlutterVIEwController = window?.rootVIEwController as! FlutterVIEwController EventChannelDemo(messenger: controller.binaryMessenger) GeneratedpluginRegistrant.register(with: self) return super.application(application,dIDFinishLaunchingWithOptions: launchOptions) }}
交流老孟Flutter博客(330个控件用法+实战入门系列文章):http://laomengit.com
欢迎加入Flutter交流群(微信:laomengit)、关注公众号【老孟Flutter】:
以上是内存溢出为你收集整理的【Flutter 混合开发】与原生通信-EventChannel全部内容,希望文章能够帮你解决【Flutter 混合开发】与原生通信-EventChannel所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)