Flutter插件开发系列教程02--用MethodChannel 实现微信注册
本帖最后由 小者 于 2019-1-13 01:43 编辑第二课 Flutter插件-利用MethodChannel实现微信注册
第一步:创建flutter插件例程
输入 flutter create --org com.xuepojie --template=plugin wechatregister
第二步:下载微信支付安卓SDK
第三步:引入SDK编写代码
第四步:完美OK
手动配置微信SDK
1.下载微信SDK2.打开插件目录 Android 创建新的文件夹 libs3.将微信SDK "wechat-sdk-android-with-mta-5.1.6.jar"放入文件夹内4.打开 Android目录下的 "build.gradle" 在底部 android {}标签内添加如下代码dependencies {
compile fileTree('libs/wechat-sdk-android-with-mta-5.1.6.jar')
}
接下来-看教程
最终代码
"main.dart"
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
static String str='1';
static var str2;
// Platform messages are asynchronous, so we initialize in an async method.
//EventChannel
Future<void> inittt() async{
str2=await No_2.register;
setState(() {
str=str2;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: new Column(
children: <Widget>[
new RaisedButton(
onPressed: () async{
//等会执行调用注册命令
inittt();
}
),
new RaisedButton(
onPressed:
(){
No_2.opwechat();
},
),
new Text(str)
],
),
),
),
);
}
}
"No_2Plugin.Java"先加载类库import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;public class No_2Plugin implements MethodCallHandler {
/** Plugin registration. */
private static IWXAPI api;
public static void registerWith(Registrar registrar) {
MethodChannel _method=new MethodChannel(registrar.messenger(),"wechat");
_method.setMethodCallHandler(new No_2Plugin());
westate(registrar);
}
static public void westate(Registrar registrar){
api=WXAPIFactory.createWXAPI(registrar.context(),"wxb4ba3c02aa476ea1",false);
}
public void onMethodCall(MethodCall call,Result result)
{
if(call.method.equals("register")){
//执行注册代码
if(api.registerApp("wxb4ba3c02aa476ea1"))
{
result.success("注册成功");
}
}
if(call.method.equals("openwechat")){
api.openWXApp();
}
}
}
"No_2.dart"
class No_2 {
static MethodChannel _method=new MethodChannel('wechat');
static Future<String> get register async{
final String success=await _method.invokeMethod('register');
print(success.toString()) ;
return (success.toString());
}
static opwechat(){
_method.invokeMethod('openwechat');
}
}
注册成功{:7_238:}
回复下载
**** Hidden Message *****
谢谢分享{:5_117:} 感谢楼主分享
虽然看不懂 我来学习学习 感谢楼主分享
虽然看不懂 感谢分享!支持 谢谢分享{:5_117:} 刚学flutter,正好用到这个,真是及时雨 我来学习一下 看不懂,要学习的空间还是很大的呀{:5_191:}