Flutter Apple Sign In相關插件pub搜了一下仍是蠻多的,可是仍是忍不住要再擼一個,順即可以再熟悉下flutter plugin和 platform view 的用法(調用系統的蘋果登陸按鈕須要用到),這個自定義button widget,調用方法拿回調則不用,區別只是在於UI。另外Plugin層是基於Swift調用Apple Sign In Api實現的,也能夠順便增強下Swift,畢竟一直OC用得較多。git
pub方式:github
dependencies:
sign_in_apple: ^0.0.1
複製代碼
github:bash
效果如圖:async
The System Button Style Use Platform View to Show in Flutter.ide
由於系統按鈕使用platform view 實現ui
Please set io.flutter.embedded_views_preview = true in Info.plist in your project.spa
請在Info.plist 加入 io.flutter.embedded_views_preview = true, 同時確保項目開啓了AppleSignIn 的配置。詳情能夠下載github上example查看插件
CallBack回調:code
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,
)
複製代碼