




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 新能源汽车市场营销策略考试题及答案
- 2025年度企业安全生产知识竞赛题库及答案(共197题)
- 电商种草经济崛起下的内容营销策略研究报告
- 空间分析考试题及答案
- 数字彼此关系试题及答案
- 安全工程师考试组织能力试题及答案
- 气瓶使用管理试题及答案
- 2025房地产工程管理面试试题及答案
- 综合岗位面试试题及答案
- 教育行业2025年并购整合策略:产业链升级与创新路径研究报告
- 韦氏测试题及答案
- 历年贵州特岗试题及答案
- 2025怎样正确理解全过程人民民主的历史逻辑、实践逻辑与理论逻辑?(答案3份)
- GB/T 45501-2025工业机器人三维视觉引导系统通用技术要求
- 财务英文词汇大全
- 《基于绘本阅读的幼儿语言能力发展研究(论文)》9300字
- 印巴战争(修改稿)
- 工程项目管理实施方案(5篇)
- 2021年全国质量奖现场汇报材料-基础设施、设备及设施管理过程课件
- 防爆电气失爆判别标准和常见失爆现象汇总
- 10kV高压开关柜整定计算书
评论
0/150
提交评论