flutter_ali_oauth 0.0.4 copy "flutter_ali_oauth: ^0.0.4" to clipboard
flutter_ali_oauth: ^0.0.4 copied to clipboard

flutter_ali_oauth

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_ali_oauth/flutter_ali_oauth.dart';

import 'package:flutter_ali_oauth/model/token_result.dart';
import 'package:flutter_ali_oauth_example/onekey/OneKeyLoginHelper.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


/// 一键登录秘钥
const IosAliAuthSdk = '';
const AndroidAuthSdk = '';


class _MyAppState extends State<MyApp> {
  BuildContext mContext;
  final String TAG = "_MyAppState";
  FToast fToast;

  @override
  void initState() {
    super.initState();
    fToast = FToast();
    fToast.init(context);
    // 初始化插件
    OneKeyLoginHelper.init();
    //View点击事件监听
    OneKeyLoginHelper.setViewListener(_onViewEvent);
    //Token回调事件监听
    OneKeyLoginHelper.setTokenListener(_onTokenSuccess);
  }

  void _onViewEvent(Object event) {
    print("$TAG :_onViewEvent" + event);
    var viewMessage = AliOauthUtils.toViewMessage(event);
    int position = viewMessage.position;
    switch (position) {
      case ClickEvent.wechat:
        print("$TAG :position" + position.toString());
        _showToast("点击微信");
        break;
      case ClickEvent.qq:
        print("$TAG :position" + position.toString());
        _showToast("点击qq");
        break;
      case ClickEvent.weibo:
        print("$TAG :position" + position.toString());
        _showToast("点击微博");
        break;
      case ClickEvent.otherLogin:
        print("$TAG :position" + position.toString());
        _showToast("其他方式登录");
        break;
      case ClickEvent.back:
        print("$TAG :position" + position.toString());
        _showToast("返回");
        break;
      case ClickEvent.otherPhoneVerify:
        print("$TAG :position" + position.toString());
        _showToast("其他手机号验证");
        break;
    }
  }

  void _onTokenSuccess(Object tokenResult) {
    print("$TAG :_onTokenSuccess" + tokenResult);
    TokenResult result = AliOauthUtils.toTokenResult(tokenResult);
    if(result.success){
      if (result.code == FlutterAliOauth.CODE_GET_TOKEN_SUCCESS) {
        //假装请求服务器
        Future.delayed(Duration(seconds: 2), () {
          OneKeyLoginHelper.hideLoading();
          _showToast("登录成功");
        });
      }
    }else{

    }
  }

  //登录
  void _quickLogin() async {
        OneKeyLoginMessage loginMessage=OneKeyLoginMessage(
          loginBtn: "本机一键登录",
          otherLogin: "其他方式登录",
          prePrivacy: "登录注册代表您已同意 ",
          onePrivacy: "用户协议",
          onePrivacyUrl: "https://www.baidu.com",
          twoPrivacy: "隐私政策",
          twoPrivacyUrl: "https://www.baidu.com"
        );
        OneKeyLoginHelper.login(loginMessage);
  }

  //验证
  void _quickVerify() async {
        OneKeyVerifyMessage oneKeyVerifyMessage=OneKeyVerifyMessage(
          phoneVerify: "手机验证",
          phoneVerifyTips: "为了您的账号安全需要验证一个手机号,未注册手机号验证后自动注册",
          verifyBtn: "本机号码一键验证",
          otherVerify: "其他手机号验证"
            );
        OneKeyLoginHelper.verify(oneKeyVerifyMessage);
  }

  void _check() async{
    bool isCheck=await OneKeyLoginHelper.checkVerifyEnable();
    print("$TAG :check:$isCheck" );
  }

  void _getToken() async{
    String token=await OneKeyLoginHelper.getVerifyToken();
    print("$TAG :token:$token");
  }

  @override
  Widget build(BuildContext context) {
    mContext = context;
    return Scaffold(
      appBar: AppBar(
        title: const Text('阿里云一键登录'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
          RaisedButton(
            onPressed: () async {
              _quickLogin();
            },
            child: Text('登录'),
          ),
          RaisedButton(
            onPressed: () async {
              _quickVerify();
            },
            child: Text('验证'),
          ),
            RaisedButton(
              onPressed: () async {
               _check();
              },
              child: Text('检查是否可用'),
            ),
            RaisedButton(
              onPressed: () async {
                _getToken();
              },
              child: Text('获取token'),
            ),
        ],)

      ,
      ),
    );
  }


  /**
   * 不离开一键登录界面,toast会被一键登录界面挡住,因为context是属于上一个界面的,显示在上一个界面上。
   * 返回和其他方式登录有toast显示
   */
  _showToast(String text) {
    Widget toast = Container(
      padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.0),
        color: Colors.greenAccent,
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(Icons.check),
          SizedBox(
            width: 12.0,
          ),
          Text(text),
        ],
      ),
    );
    fToast.showToast(
      child: toast,
      gravity: ToastGravity.CENTER,
      toastDuration: Duration(seconds: 1),
    );
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

flutter_ali_oauth

Homepage

License

BSD-2-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_ali_oauth