当你在开发flutter应用的时候,有时会须要调用native的api,每每遇到flutter并无相应的package, 这时候flutter plugin就开始发挥做用了,这篇文章将会讲解开发一个简单flutter plugin的步骤和方法,好了,让咱们开始动手吧。前端
1.在Android Studio 中建立一个Flutter Plugin 项目,以下图java
上图中你能看到项目描述中写到,若是须要暴露Andorid或iOS的API给开发者时,选择"Plugin"项目类型。
这个项目咱们命名为:flutter_native_log_plugin, 当咱们完成建立项目后,有两个文件咱们须要看一看, 一个是位于android/src下的FlutterNativeLogPlugin.java, 这段代码是用来和本地设备交互,而后将交互结果返回供flutter前端调用, 以下所示:android
package com.cube8.flutter_native_log_plugin; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; /** FlutterNativeLogPlugin */ public class FlutterNativeLogPlugin implements MethodCallHandler { /** Plugin registration. */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_native_log_plugin"); channel.setMethodCallHandler(new FlutterNativeLogPlugin()); } @Override public void onMethodCall(MethodCall call, Result result) { if (call.method.equals("getPlatformVersion")) { result.success("Android " + android.os.Build.VERSION.RELEASE); } else { result.notImplemented(); } } }
另外一个 /lib/mian.dart文件,这段代码是主要用来和native代码交互, 以下所示:api
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. app
在lib/flutter_native_log_plugin.dart 文件中,咱们先建立一个新的方法,代码以下:async
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改写以下:ide
package com.cube8.flutter_native_log_plugin; import android.util.Log; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; /** * FlutterNativeLogPlugin */ public class FlutterNativeLogPlugin implements MethodCallHandler { /** * Plugin registration. */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_native_log_plugin"); channel.setMethodCallHandler(new FlutterNativeLogPlugin()); } @Override public void onMethodCall(MethodCall call, Result result) { if (call.method.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 { result.notImplemented(); } } }
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 } @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能够顺利运行了。ui
4.最后一步就是将咱们开发的plugin发布到dart pub供之后直接调用。打开控制台,须要确认定位到plugin项目的根目录,而后输入以下命令:spa
flutter packages pub publish --dry-run
这段命令会作一个程序相关文件和信息的检查,确保待发布的plugin信息完整,根据控制台的提示完善信息后,与下图类似:
接着输入以下命令,正式将plugin发布到dart pub中:
flutter packages pub publish
若是你喜欢这篇文章或者认为有收获,请给这篇文章点个赞,你的支持就是笔者继续努力写文章的动力。