小者 发表于 2019-1-13 01:39

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 *****






小白的烦恼 发表于 2019-1-13 16:42

谢谢分享{:5_117:}

HlccFu 发表于 2019-1-18 22:16

感谢楼主分享
虽然看不懂

yhmxpj 发表于 2019-1-20 13:03

我来学习学习

stefma 发表于 2019-1-22 11:50

感谢楼主分享
虽然看不懂

未来路丶 发表于 2019-3-2 16:01

感谢分享!支持

zhuochujie 发表于 2019-3-11 23:46

谢谢分享{:5_117:}

tomkiller 发表于 2019-3-30 08:16

刚学flutter,正好用到这个,真是及时雨

magicwk 发表于 2019-4-15 19:28

我来学习一下

xu983206 发表于 2019-5-11 15:38

看不懂,要学习的空间还是很大的呀{:5_191:}
页: [1] 2 3
查看完整版本: Flutter插件开发系列教程02--用MethodChannel 实现微信注册