撸一个Flutter Apple Id登录插件

### 前言

Flutter Apple Sign In相关插件pub搜了一下还是蛮多的,但是还是忍不住要再撸一个,顺便可以再熟悉下flutter plugin和 platform view 的用法(调用系统的苹果登录按钮需要用到),这个自定义button widget,调用方法拿回调则不用,区别只是在于UI。

下载地址

pub方式:

dependencies:
  sign_in_apple: ^0.0.1

github:

https://github.com/JerryFans/sign_in_apple

效果如图:


Usage

The System Button Style Use Platform View to Show in Flutter.

因为系统按钮使用platform view 实现

Please set io.flutter.embedded_views_preview = true in Info.plist in your project.

请在Info.plist 加入 io.flutter.embedded_views_preview = true,
同时确保项目开启了AppleSignIn 的配置。详情可以下载github上example查看

CallBack回调:

SignInApple.handleAppleSignInCallBack(onCompleteWithSignIn: (String name,
        String mail,
        String userIdentifier,
        String authorizationCode,
        String identifyToken) async {
      print("flutter receiveCode: \n");
      print(authorizationCode);
      print("flutter receiveToken \n");
      print(identifyToken);
      setState(() {
        _name = name;
        _mail = mail;
        _userIdentify = userIdentifier;
        _authorizationCode = authorizationCode;
      });
    }, onCompleteWithError: (AppleSignInErrorCode code) async {
      var errorMsg = "unknown";
      switch (code) {
        case AppleSignInErrorCode.canceled:
          errorMsg = "user canceled request";
          break;
        case AppleSignInErrorCode.failed:
          errorMsg = "request fail";
          break;
        case AppleSignInErrorCode.invalidResponse:
          errorMsg = "request invalid response";
          break;
        case AppleSignInErrorCode.notHandled:
          errorMsg = "request not handled";
          break;
        case AppleSignInErrorCode.unknown:
          errorMsg = "request fail unknown";
          break;
      }
      print(errorMsg);
    });

UI相关:

自定义Widget按钮直接触发plugin按钮事件即可:

GestureDetector(
              onTap: () {
                SignInApple.clickAppleSignIn();
              },
              child: Container(
                width: 56,
                height: 56,
                child: Image.asset(
                  "images/apple_logo.png",
                  width: 56,
                  height: 56,
                ),
              ),
            ),

系统ButtonWidget直接显示,组件内部已经实现了事件,点击了会触发上面的回调:

AppleSignInSystemButton(
              width: 250,
              height: 50,
              buttonStyle: AppleSignInSystemButtonStyle.black,
)

发表评论

电子邮件地址不会被公开。 必填项已用*标注