基于Flutter Version:1.22.3, Dart Version:2.10.3
FlutterPlugin提供了Android和ios的底层封装,在Flutter层提供组件功能,使得Flutter可以比较方便得调用Native得模块,对于Flutter实现比较复杂或者基于平台不能完成得部分,都可以封装成Plugin。
本文都是android得样例。
Flutter调用android原生实现,封装成Plugin
基本流程
- Flutter端声明MethodChannel,并通过invokeMethod函数调用native的函数
- Android声明Plugin,创建与Flutter端对应的MethodChannel,对Flutter端声明的函数进行监听,并进行相应的处理
- 在Plugin机制中绑定两者的关系。
此处以调用Android原生的日志为例进行讲解
1.Flutter端声明一个log_util.dart文件
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class LogUtils {
///声明的通道名称
static const String _channelName = 'flutter.io/system/log';
///判断是否是debug环境
static bool get _isDebug => !bool.fromEnvironment("dart.vm.product");
///创建渠道对象
static const _channel = const MethodChannel(_channelName);
///通过命名函数来实现类似java中重载机制
static void v({@required String message, String tag}) {
if (_isDebug) {
///调用函数来执行,
///invokeMethod参数
///[String method]:指定方法名,Android端需要监听并做处理的
///[dynamic arguments]:需要传递给Android端的参数
_channel.invokeMethod('logV', {'tag': tag ?? '', 'message': message ?? ''});
}
}
static void d({@required String message, String tag}) {
if (_isDebug) {
_channel.invokeMethod('logD', {'tag': tag ?? '', 'message': message ?? ''});
}
}
static void i({@required String message, String tag}) {
if (_isDebug) {
_channel.invokeMethod('logI', {'tag': tag ?? '', 'message': message ?? ''});
}
}
static void w({@required String message, String tag}) {
if (_isDebug) {
_channel.invokeMethod('logW', {'tag': tag ?? '', 'message': message ?? ''});
}
}
static void e({@required String message, String tag}) {
if (_isDebug) {
_channel.invokeMethod('logE', {'tag': tag ?? '', 'message': message ?? ''});
}
}
}
2.Android端plugin的编写
在android端的项目中有一个目录如下,
image.png
package io.flutter.plugins;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
/**
* 打印日志plugin
*/
public class LogPlugin implements FlutterPlugin , MethodChannel.MethodCallHandler{
private static final String mChannelName = "flutter.io/system/log";//与Flutter端对应的通道名称
private static final String LOG_V = "logV";
private static final String LOG_D = "logD";
private static final String LOG_I = "logI";
private static final String LOG_W = "logW";
private static final String LOG_E = "logE";
private MethodChannel mMethodChannel;
private Context mApplicationContext;
/**
* 当plugin与FlutterEngine相关联时调用
* @param binding
*/
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
//获取上下文对象,可以发起跳转Activity等操作
mApplicationContext = binding.getApplicationContext();
//创建通道对象
mMethodChannel = new MethodChannel(binding.getBinaryMessenger(),mChannelName);
//添加对Flutter端调用函数的监听
mMethodChannel.setMethodCallHandler(this);
}
/**
* 当plugin与FlutterEngine解除关联时调用,用来清除资源等。
* @param binding
*/
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
mApplicationContext = null;
mMethodChannel.setMethodCallHandler(null);
mMethodChannel = null;
}
/**
* 对Flutter端调用函数的监听
* @param call
* @param result
*/
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
//获取传递过来的参数
String tag = call.argument("tag");
String message = call.argument("message");
//获取Flutter端调用的函数名称
String method = call.method;
switch (method){
case LOG_V:
Log.v(tag,message);
break;
case LOG_D:
Log.d(tag,message);
break;
case LOG_I:
Log.i(tag,message);
break;
case LOG_W:
Log.w(tag,message);
break;
case LOG_E:
Log.e(tag,message);
break;
}
}
}
3.在FlutterEngine的plugin中添加LogPlugin建立关联
package com.example.flutter_first;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.LogPlugin;
public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
flutterEngine.getPlugins().add(new LogPlugin());
}
}
调用如下:
image.png
网友评论