⽤Flutter开发⾃定义Plugin的⽅法⽰例
当你在开发flutter应⽤的时候,有时会需要调⽤native的api,往往遇到flutter并没有相应的package,这时候flutter plugin就开始发挥作⽤了,这篇⽂章将会讲解开发⼀个简单flutter plugin的步骤和⽅法,好了,让我们开始动⼿吧。
1.在Android Studio 中创建⼀个Flutter Plugin 项⽬,如下图
上图中你能看到项⽬描述中写到,如果需要暴露Andorid或iOS的API给开发者时,选择"Plugin"项⽬类型。
这个项⽬我们命名为:flutter_native_log_plugin, 当我们完成创建项⽬后,有两个⽂件我们需要看⼀看,⼀个是位于android/src 下的FlutterNativeLogPlugin.java,这段代码是⽤来和本地设备交互,然后将交互结果返回供flutter前端调⽤,如下所⽰:
package com.cube8.flutter_native_log_plugin;
import io.flutter.pluginmon.MethodCall;
import io.flutter.pluginmon.MethodChannel;
import io.flutter.pluginmon.MethodChannel.MethodCallHandler;
import io.flutter.pluginmon.MethodChannel.Result;
import io.flutter.pluginmon.PluginRegistry.Registrar;
/** FlutterNativeLogPlugin */
public class FlutterNativeLogPlugin implements MethodCallHandler {
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new ssenger(),
"flutter_native_log_plugin");
channel.setMethodCallHandler(new FlutterNativeLogPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (hod.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
}
}
另⼀个 /lib/mian.dart⽂件,这段代码是主要⽤来和native代码交互,如下所⽰:import 'dart:async';
import 'package:flutter/services.dart';
class FlutterNativeLogPlugin {
static const MethodChannel _channel =
const MethodChannel('flutter_native_log_plugin');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
2.现在我们开始编写我们的Plugin.
在lib/flutter_native_log_plugin.dart ⽂件中,我们先创建⼀个新的⽅法,代码如下:import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
enum Log { DEBUG, WARNING, ERROR }
class FlutterNativeLogPlugin {
static const MethodChannel _channel =
const MethodChannel('flutter_native_log_plugin');
static Future<String> printLog(
{Log logType, @required String tag, @required String msg}) async {
String log = "debug";
if (logType == Log.WARNING) {
log = "warning";
} else if (logType == Log.ERROR) {
log = "error";
} else {
log = "debug";
}
final Map<String, dynamic> params = <String, dynamic>{
'tag': tag,
'msg': msg,
'logType': log
};
final String result = await _channel.invokeMethod('printLog', params);
return result;
}
}
在Android端,我们将android/src下的FlutterNativePlugin.java改写如下:
package com.cube8.flutter_native_log_plugin;
import android.util.Log;
import io.flutter.pluginmon.MethodCall;
import io.flutter.pluginmon.MethodChannel;
import io.flutter.pluginmon.MethodChannel.MethodCallHandler;
import io.flutter.pluginmon.MethodChannel.Result;
import io.flutter.pluginmon.PluginRegistry.Registrar;
/**
* FlutterNativeLogPlugin
*/
public class FlutterNativeLogPlugin implements MethodCallHandler {
/**
* Plugin registration.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new ssenger(), "flutter_native_log_plugin");
channel.setMethodCallHandler(new FlutterNativeLogPlugin());
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (hod.equals("printLog")) {
String msg = call.argument("msg");
String tag = call.argument("tag");
String logType = call.argument("logType");
if (logType.equals("warning")) {
Log.w(tag, msg);
} else if (logType.equals("error")) {
Log.e(tag, msg);
} else {
Log.d(tag, msg);
}
result.success("Logged Successfully!");
} else {
}
}
}
3.测试plugin。当开发完了我们的plugin之后,我们需要测试这个新plugin是否可⽤,于是对example/lib的main.dart⽂件作如下修改:
import 'package:flutter/material.dart';
import 'package:flutter_native_log_plugin/flutter_native_log_plugin.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
void printLogs() async {
print(await FlutterNativeLogPlugin.printLog(
tag: "Debug", msg: "This is ordinary Log")); // default logType
print(await FlutterNativeLogPlugin.printLog(
tag: "Debug",
msg: "This is warning Log",
logType: Log.WARNING)); // logType = warning
print(await FlutterNativeLogPlugin.printLog(
tag: "Debug",
msg: "This is error Log",
logType: Log.ERROR)); // logType = error
print(await FlutterNativeLogPlugin.printLog(
tag: "Debug",
msg: "This is debug Log",
logType: Log.DEBUG)); // logType = debug
}
flutter开发app@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: RaisedButton(
child: Text("PrintLogs"),
onPressed: printLogs,
),
),
);
}
}
点击app中的按钮,控制台将看到如下输出,说明plugin可以顺利运⾏了。
4.最后⼀步就是将我们开发的plugin发布到dart pub供以后直接调⽤。打开控制台,需要确认定位到plugin项⽬的根⽬录,然后输⼊如下命令:
flutter packages pub publish --dry-run
这段命令会做⼀个程序相关⽂件和信息的检查,确保待发布的plugin信息完整,根据控制台的提⽰完善信息后,与下图相似:
接着输⼊如下命令,正式将plugin发布到dart pub中:
flutter packages pub publish
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。