




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Flutter中自定义Plugin
怎么在Flutter中自定义Plugin?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面在下将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1.在AndroidStudio中创建一个FlutterPlugin项目,如下图上图中你能看到项目描述中写到,如果需要暴露Andorid或iOS的API给开发者时,选择"Plugin"项目类型。这个项目我们命名为:flutter_native_log_plugin,当我们完成创建项目后,有两个文件我们需要看一看,一个是位于android/src下的FlutterNativeLogPlugin.java,这段代码是用来和本地设备交互,然后将交互结果返回供flutter前端调用,如下所示:package
com.cube8.flutter_native_log_plugin;
import
mon.MethodCall;
import
mon.MethodChannel;
import
mon.MethodChannel.MethodCallHandler;
import
mon.MethodChannel.Result;
import
mon.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.现在我们开始编写我们的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
mon.MethodCall;
import
mon.MethodChannel;
import
mon.MethodChannel.MethodCallHandler;
import
mon.MethodChannel.Result;
import
mon.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中的按钮,控
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 14496-10:2025 EN Information technology - Coding of audio-visual objects - Part 10: Advanced video coding
- 单位工程划分课件编写
- 赫山区考编数学试卷
- 黄陂四年级数学试卷
- 汉中龙岗初一数学试卷
- 2025年山东省齐河县一中高一物理第二学期期末质量跟踪监视模拟试题含解析
- 2025年中国旋转吊具行业市场发展前景及发展趋势与投资战略研究报告
- 2025年卫浴树脂项目可行性分析报告
- 2025年高空作业车项目投资分析及可行性报告
- 高炉布袋除尘器项目投资可行性研究分析报告(2024-2030版)
- 内燃机车柴油机 课件 2-1-6 16V280型柴油机调控系统认知
- 小学四年级道德与法治期末考试质量分析
- 呼吸科利用PDCA循环提高肺功能检查结果达标率品管圈QCC成果汇报
- 钳工实操试卷-共44套
- 岭南版八年级下册美术 6色彩的表现 课件
- 07FK02防空地下室通风设备安装图集
- 麻醉药品精神药品管理培训课件
- 麻精药品培训课件
- 医院全员聘用制度和岗位聘任管理制度
- 14 《中国胰岛素泵治疗指南(2021年版)》要点解读
- 12J4-2 《专用门窗》标准图集
评论
0/150
提交评论