欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Flutter中怎么自定義Plugin

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

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

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

上圖中你能看到項目描述中寫到,如果需要暴露Andorid或iOS的API給開發(fā)者時,選擇"Plugin"項目類型。這個項目我們命名為:flutter_native_log_plugin, 當(dāng)我們完成創(chuàng)建項目后,有兩個文件我們需要看一看, 一個是位于android/src下的FlutterNativeLogPlugin.java, 這段代碼是用來和本地設(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();  } }}

另一個 /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.現(xiàn)在我們開始編寫我們的Plugin.

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

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.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。當(dāng)開發(fā)完了我們的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可以順利運行了。

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

flutter packages pub publish --dry-run

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

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

flutter packages pub publish

以上就是Flutter中怎么自定義Plugin,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:Flutter中怎么自定義Plugin
分享URL:http://aaarwkj.com/article42/ihhjec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、外貿(mào)建站、面包屑導(dǎo)航品牌網(wǎng)站設(shè)計、做網(wǎng)站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
中文字幕乱码亚洲中文在线| 日本一区二区三区免费精品| 欧美美女午夜福利视频| 亚洲欧美精品专区极品| 国内久久婷婷综合五月趴| 侵犯人妻中文字幕一区二区| 日韩欧美黄色三级视频| 国产夫妻一区二区三区| 91精品蜜臀国产综合久久久久久| 人妻少妇久久久久久69| 日日骚岛国中文字幕av| 毛片精品一区二区二区三区| 精精国产xxxx视频在线不卡| 欧美精品成人在线一区| 亚洲午夜一区二区三区精品影院| 激情毛片av在线免费看| 国产美女无遮挡免费网站| 最新国产毛片久热精品视频| 亚洲三级黄片免费播放| 国产日韩欧美亚洲中文| 亚洲av天堂一区二区香蕉| 91精品婷婷国产综合| 久久人妻一区二区三区免费密臀 | 韩日男人女人性生活视频| 久久精品国产亚洲av久| 国语精品对白交换日韩| 久久国产精品午夜视频| 国产b片免费在线观看| 欧美黑人在线一区二区| 九九re久久这里有精品| 成年女人毛片免费观看不卡| 欧美中文日韩国产字幕| 91蜜臀视频在线播放| 精品人妻人伦一区二区三区| 精品自拍一区在线观看| 国产乱肥老妇国产一区二| 国产三级自拍视频在线观看| 一区二区三区日韩欧美在线| 国产黄片大秀在线观看| 欧美国内日本一区二区| 欧美黄片免费在线视频|