版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 开放性特指腕骨骨折的健康教育
- 金色的草地的评课稿大全
- 物理第六章第二节《密度》教学设计
- 产业扶持基金借款合同
- 室内装修合同样本-装修合同(协议)范本
- 福建房屋市政工程风险分级主要管控清单、危险性较大分部分项工程清单
- 项目评估:招商引资项目的风险与陷阱
- 进修人员登记表
- 2023年多功能气象卫星接收系统资金需求报告
- 快递服务合同5篇
- 我国果冻的研究进展
- 铁路运输信息化与数字转型实践
- 小学信息技术《信息的输入和输出》课件
- 给排水工程仪表与控制讲稿
- 煤矿企业法人代表安全生产责任制范本
- 济南市七年级下学期期中考试数学试卷及答案解析(共两套)
- JT∕T 1178.1-2023 营运货车安全技术条件 第1部分:载货汽车
- 机器人技术在酒店服务中的应用趋势
- 2021年广东高考历史试卷真题及答案
- 文物评估报告
- 冀教版五年级英语下册教学课件 Lesson 20
评论
0/150
提交评论