Flutter通信插件fl_channel的使用
Flutter通信插件fl_channel的使用
fl_channel
FlEvent()
EventChannel
封装
以下是一个完整的示例,展示了如何在Flutter应用中使用fl_channel
插件进行通信。
示例代码
import 'package:example/src/event.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
theme: ThemeData.light(useMaterial3: true),
darkTheme: ThemeData.dark(useMaterial3: true),
debugShowCheckedModeBanner: false,
title: 'FlChannel',
home: const App()));
}
class App extends StatelessWidget {
const App({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBarText('FlChannel'),
body: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const SizedBox(width: double.infinity),
ElevatedText(
text: 'FlEvent',
onPressed: () {
showCupertinoModalPopup(
context: context, builder: (_) => const FlEventPage());
}),
]));
}
}
class TextBox extends StatelessWidget {
const TextBox(this.keyName, this.value, {super.key});
final dynamic keyName;
final dynamic value;
[@override](/user/override)
Widget build(BuildContext context) {
return Visibility(
visible: value != null &&
value.toString().isNotEmpty &&
value.toString() != 'null',
child: Container(
margin: const EdgeInsets.all(8), child: Text('$keyName = $value')));
}
}
class ElevatedText extends ElevatedButton {
ElevatedText({super.key, required String text, required super.onPressed})
: super(child: Text(text));
}
class AppBarText extends AppBar {
AppBarText(String text, {super.key})
: super(
elevation: 0,
title: Text(text,
style:
const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
centerTitle: true);
}
更多关于Flutter通信插件fl_channel的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter通信插件fl_channel的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter中使用fl_channel
(通常指的是平台通道,即Platform Channel)进行通信的代码示例。由于fl_channel
并不是一个特定的库名,这里我们将展示如何使用MethodChannel和BasicMessageChannel这两种常见的平台通道。
1. MethodChannel 示例
MethodChannel用于在Flutter和原生平台(Android和iOS)之间调用方法。
Flutter 端代码
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const platform = MethodChannel('com.example.myapp/channel');
String _result = 'No result';
Future<void> _invokeNativeMethod() async {
try {
final String result = await platform.invokeMethod('getBatteryLevel');
setState(() {
_result = result;
});
} on PlatformException catch (e) {
setState(() {
_result = "Failed to invoke: '${e.message}'.";
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('MethodChannel Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Battery level: $_result'),
SizedBox(height: 20),
ElevatedButton(
onPressed: _invokeNativeMethod,
child: Text('Get Battery Level'),
),
],
),
),
),
);
}
}
Android 端代码(MainActivity.kt)
package com.example.myapp
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "com.example.myapp/channel"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "getBatteryLevel") {
val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val batteryLevel: Int = batteryManager.intProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
result.success(batteryLevel.toString())
} else {
result.notImplemented()
}
}
}
}
iOS 端代码(AppDelegate.swift)
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "com.example.myapp/channel", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) in
if call.method == "getBatteryLevel" {
UIDevice.current.batteryLevel? { batteryLevel in
result(batteryLevel)
}
} else {
result(FlutterMethodNotImplemented)
}
})
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
2. BasicMessageChannel 示例
BasicMessageChannel用于在Flutter和原生平台之间发送和接收消息,这些消息可以是任意类型的对象(通过编码和解码)。
Flutter 端代码
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const BasicMessageChannel<String> _channel = BasicMessageChannel<String>(
'com.example.myapp/messageChannel',
StringCodec(),
);
TextEditingController _controller = TextEditingController();
@override
void initState() {
super.initState();
_channel.setMessageHandler((message) async {
setState(() {
_controller.text += '\nFrom Native: $message';
});
});
}
Future<void> _sendMessage() async {
String message = _controller.text;
_channel.send(message);
setState(() {
_controller.text += '\nSent to Native: $message';
_controller.clear();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BasicMessageChannel Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(
controller: _controller,
maxLines: 6,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter message',
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _sendMessage,
child: Text('Send Message'),
),
SizedBox(height: 20),
Expanded(
child: SingleChildScrollView(
child: Text(
_controller.text,
style: TextStyle(fontSize: 16),
),
),
),
],
),
),
),
);
}
}
Android 端代码(MainActivity.kt)
package com.example.myapp
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.StringCodec
class MainActivity: FlutterActivity() {
private val CHANNEL = "com.example.myapp/messageChannel"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
val channel = BasicMessageChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL, StringCodec())
channel.setMessageHandler { message, reply ->
// Handle message from Flutter
val response = "Received: $message"
reply.reply(response)
}
// Optionally, you can send a message to Flutter from native code here
// channel.send("Hello from Android!")
}
}
iOS 端代码(AppDelegate.swift)
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let channel = FlutterBasicMessageChannel<String>(name: "com.example.myapp/messageChannel", binaryMessenger: controller.binaryMessenger, codec: FlutterStringCodec.sharedInstance())
channel.setMessageHandler({ (message, reply) in
// Handle message from Flutter
let response = "Received: \(message!)"
reply(response)
})
// Optionally, you can send a message to Flutter from native code here
// channel.send("Hello from iOS!")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
这些示例展示了如何在Flutter和原生平台之间使用MethodChannel和BasicMessageChannel进行通信。你可以根据实际需求调整这些代码。