TreeviewCopyright © aleen42 all right reserved, powered by aleen42
在前一篇文章中,我们在 Flutter 端让 Android 端发出了 Toast,但内容是在 Android 端定义的,如何从 Flutter 端传递参数到 Android 呢?
Flutter to Android
在 Flutter 端 MethodChannel
的 invokeMethod
方法还有第二个参数:
@optionalTypeArgs
Future<T> invokeMethod<T>(String method, [ dynamic arguments ]) {
return _invokeMethod<T>(method, missingOk: false, arguments: arguments);
}
它接收一个 Map,我们要传递到 Android 端的数据以键值对的形式进行传递,例如:
String result = await _channel.invokeMethod('showToast', {
"content": message,
"number": 1233,
});
Android to Flutter
在 Android 端向 Flutter 发送数据也十分简单,直接调用 Result
对象的 success
方法即可:
result.success("已经发送成功");
可以传递的数据结构
在 Flutter 和 Android/iOS 之间并不是所有数据类型都可以传递的,只有以下可以
Dart | Android | iOS |
---|---|---|
null | null | nil(NSNull when nested) |
bool | Java.lang.Boolean | NSNumber numberWithBool: |
int | Java.lang.Integer | NSNumber numberWithInt: |
int, if 32 bits not enough | Java.lang.Long | NSNumber numberWithLong: |
int, if 64 bits not enough | Java.lang.BigInteger | FlutterStandardBigInteger |
double | Java.lang.Double | NSNumber numberWithDouble |
String | java.lang.String | NSString |
Unit8List | byte[] | FlutterStandardTypedData typedDataWithBytes: |
Int32List | int[] | FlutterStandardTypedData typedDataWithInt32: |
Int64List | long[] | FlutterStandardTypedData typedDataWithInt64: |
Float64List | double[] | FlutterStandardTypedData typedDataWithFloat64: |
List | Java.util.ArrayList | NSArray |
Map | Java.util.HashMap | NSDictionary |
示例
android_toast_plugin.dart
import 'dart:async';
import 'package:flutter/services.dart';
class AndroidToastPlugin {
static const MethodChannel _channel =
const MethodChannel('ANDROID_TOAST_PLUGIN');
static Future<String> showToast(String message) async {
String result = await _channel.invokeMethod('showToast', {
"content": message,
"number": 1233,
});
return result;
}
}
AndroidToastPlugin.java
public class AndroidToastPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "ANDROID_TOAST_PLUGIN");
channel.setMethodCallHandler(this);
context = flutterPluginBinding.getApplicationContext();
}
static FlutterView view;
static Context context;
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "ANDROID_TOAST_PLUGIN");
channel.setMethodCallHandler(new AndroidToastPlugin());
context = registrar.context();
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("showToast")) {
String message = call.argument("content");
int number = call.argument("number");
Toast.makeText(context,message + number,Toast.LENGTH_SHORT).show();
result.success("已经发送成功");
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
使用插件代码,main.dart
import 'package:flutter/material.dart';
import 'package:lixyz/android_taost_plugin.dart';
import 'package:flutter/services.dart';
import 'dart:async';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String result = "嘻嘻嘻";
Future<void> sendToastMessage(String message) async {
try {
result = await AndroidToastPlugin.showToast(message);
print(result);
} on PlatformException {
result = 'result is null';
}
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: <Widget>[
Text(result),
Center(
child: MaterialButton(
child: Text("点击弹出 Toast"),
color: Colors.blue,
onPressed: () {
sendToastMessage("这是要显示的内容");
},
),
),
],
),
),
);
}
}