用Flutter開發自定義Plugin

當你在開發flutter應用的時候,有時會須要調用native的api,每每遇到flutter並無相應的package, 這時候flutter plugin就開始發揮做用了,這篇文章將會講解開發一個簡單flutter plugin的步驟和方法,好了,讓咱們開始動手吧。前端

1.在Android Studio 中建立一個Flutter Plugin 項目,以下圖java

clipboard.png

上圖中你能看到項目描述中寫到,若是須要暴露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,
          ),
        ),
      ),
    );
  }
}

clipboard.png

點擊app中的按鈕,控制檯將看到以下輸出,說明plugin能夠順利運行了。ui

clipboard.png

4.最後一步就是將咱們開發的plugin發佈到dart pub供之後直接調用。打開控制檯,須要確認定位到plugin項目的根目錄,而後輸入以下命令:spa

flutter packages pub publish --dry-run

這段命令會作一個程序相關文件和信息的檢查,確保待發布的plugin信息完整,根據控制檯的提示完善信息後,與下圖類似:

clipboard.png

接着輸入以下命令,正式將plugin發佈到dart pub中:

flutter packages pub publish

若是你喜歡這篇文章或者認爲有收穫,請給這篇文章點個贊,你的支持就是筆者繼續努力寫文章的動力。

相關文章
相關標籤/搜索