Skip to main content

account_kit_ohos 0.0.1 copy "account_kit_ohos: ^0.0.1" to clipboard
account_kit_ohos: ^0.0.1 copied to clipboard

A Flutter plugin for HarmonyOS to integrate Huawei Account Quick Login.

example/lib/main.dart

import 'dart:async';

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late AccountKitButtonController buttonController;
  StreamSubscription<AccountKitResponse>? subscription;
  bool agreementStatus = false;
  String? anonymousPhone;
  String? authCode;

  @override
  void initState() {
    super.initState();
  }

  /// 获取匿名手机号(用于登录页展示)
  void getQuickLoginAnonymousPhone() async {
    AccountKitResponseString res =
        await AccountKitMethodChannel.instance.getQuickLoginAnonymousPhone();
    if (res.code == AccountKitResponseData.SUCCESS) {
      setState(() {
        anonymousPhone = res.data;
      });
    } else {
      setState(() {
        anonymousPhone = res.message;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('华为账号一键登录'),
        ),
        body: Container(
          child: platformView(),
        ),
      ),
    );
  }

  Widget platformView() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const SizedBox(height: 100),
        GestureDetector(
          onTap: getQuickLoginAnonymousPhone,
          child: Container(
            color: Colors.yellow,
            width: 300,
            height: 50,
            alignment: Alignment.center,
            child: const Text('获取虚拟手机号'),
          ),
        ),
        const SizedBox(height: 20),
        Text('anonymousPhone = ${anonymousPhone ?? ''}'),
        const SizedBox(height: 60),
        Text('authCode = ${authCode ?? ''}'),
        const SizedBox(height: 20),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 30),
          height: 40,
          child: AccountKitButton(
            onViewCreated: (controller) {
              buttonController = controller;
              subscription = controller.dataStream.listen((res) {
                if (res.type == AccountKitResponseType.agreementDialogOpen) {
                  setState(() {
                    authCode = '未同意协议';
                  });
                } else if (res.type == AccountKitResponseType.loginSuccess) {
                  AccountKitResponseString? resData =
                      res.data as AccountKitResponseString;
                  setState(() {
                    authCode = resData.data;
                  });
                } else if (res.type == AccountKitResponseType.loginError) {
                  AccountKitResponseString? resData =
                      res.data as AccountKitResponseString;
                  setState(() {
                    authCode = resData.message;
                  });
                }
              });
            },
          ),
        ),
        const SizedBox(height: 20),
        GestureDetector(
          onTap: () {
            agreementStatus = !agreementStatus;
            buttonController.agreementStatusChanged(agreementStatus);
          },
          child: Container(
            color: Colors.yellow,
            width: 100,
            height: 40,
            alignment: Alignment.center,
            child: const Text('同意协议'),
          ),
        ),
      ],
    );
  }

  @override
  void dispose() {
    subscription?.cancel();
    super.dispose();
  }
}
0
likes
55
points
98
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for HarmonyOS to integrate Huawei Account Quick Login.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on account_kit_ohos

Packages that implement account_kit_ohos