真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Flutter中怎么自定義Plugin

本篇文章給大家分享的是有關(guān)Flutter中怎么自定義Plugin,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。

創(chuàng)新互聯(lián)建站是專(zhuān)業(yè)的安吉網(wǎng)站建設(shè)公司,安吉接單;提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行安吉網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

1.在Android Studio 中創(chuàng)建一個(gè)Flutter Plugin 項(xiàng)目,如下圖

上圖中你能看到項(xiàng)目描述中寫(xiě)到,如果需要暴露Andorid或iOS的API給開(kāi)發(fā)者時(shí),選擇"Plugin"項(xiàng)目類(lèi)型。這個(gè)項(xiàng)目我們命名為:flutter_native_log_plugin, 當(dāng)我們完成創(chuàng)建項(xiàng)目后,有兩個(gè)文件我們需要看一看, 一個(gè)是位于android/src下的FlutterNativeLogPlugin.java, 這段代碼是用來(lái)和本地設(shè)備交互,然后將交互結(jié)果返回供flutter前端調(diào)用, 如下所示:

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();  } }}

另一個(gè) /lib/mian.dart文件,這段代碼是主要用來(lái)和native代碼交互, 如下所示:

import 'dart:async';import 'package:flutter/services.dart';class FlutterNativeLogPlugin { static const MethodChannel _channel =   const MethodChannel('flutter_native_log_plugin'); static Future get platformVersion async {  final String version = await _channel.invokeMethod('getPlatformVersion');  return version; }}

2.現(xiàn)在我們開(kāi)始編寫(xiě)我們的Plugin.

在lib/flutter_native_log_plugin.dart 文件中,我們先創(chuàng)建一個(gè)新的方法,代碼如下:

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 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 params = {   'tag': tag,   'msg': msg,   'logType': log  };  final String result = await _channel.invokeMethod('printLog', params);  return result; }}

在Android端,我們將android/src下的FlutterNativePlugin.java改寫(xiě)如下:

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.測(cè)試plugin。當(dāng)開(kāi)發(fā)完了我們的plugin之后,我們需要測(cè)試這個(gè)新plugin是否可用,于是對(duì)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 { @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,     ),    ),   ),  ); }}

點(diǎn)擊app中的按鈕,控制臺(tái)將看到如下輸出,說(shuō)明plugin可以順利運(yùn)行了。

4.最后一步就是將我們開(kāi)發(fā)的plugin發(fā)布到dart pub供以后直接調(diào)用。打開(kāi)控制臺(tái),需要確認(rèn)定位到plugin項(xiàng)目的根目錄,然后輸入如下命令:

flutter packages pub publish --dry-run

這段命令會(huì)做一個(gè)程序相關(guān)文件和信息的檢查,確保待發(fā)布的plugin信息完整,根據(jù)控制臺(tái)的提示完善信息后,與下圖相似:

接著輸入如下命令,正式將plugin發(fā)布到dart pub中:

flutter packages pub publish

以上就是Flutter中怎么自定義Plugin,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


文章標(biāo)題:Flutter中怎么自定義Plugin
鏈接地址:http://weahome.cn/article/ihhjec.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部