vk_id_auth 0.0.2 copy "vk_id_auth: ^0.0.2" to clipboard
vk_id_auth: ^0.0.2 copied to clipboard

discontinued

A Flutter plugin for auth via VK ID SDK. The plugin allows you to log in using VK ID SDK and receive an access token and user data. If the VK application is installed on the device, then the plugin wi [...]

example/lib/main.dart

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:vk_id_auth/entity/entity.dart';
import 'package:vk_id_auth/vk_id_auth.dart';

void main() => runApp(const App());

@immutable
class App extends StatefulWidget {
  const App({
    Key? key,
  }) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  VkIdAuthData? data;
  late final VkIdAuth _vkIdAuth;

  @override
  void initState() {
    super.initState();
    _vkIdAuth = const VkIdAuth();
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        theme: ThemeData.light(),
        darkTheme: ThemeData.dark(),
        themeMode: ThemeMode.system,
        home: FutureBuilder(
          future: _initialize(),
          builder: (context, snapshot) {
            late final Widget child;

            if (snapshot.connectionState != ConnectionState.done) {
              child = const Center(
                child: CircularProgressIndicator(),
              );
            } else {
              child = VkIdAuthPage(
                data: data,
                loginCallback: _login,
              );
            }

            return AnimatedSwitcher(
              duration: const Duration(milliseconds: 250),
              child: child,
            );
          },
        ),
      );

  Future<void> _initialize() async {
    try {
      final isInitialized = await _vkIdAuth.isInitialized;
      if (!isInitialized) {
        await _vkIdAuth.initialize();
      }
    } on Object catch (error, stackTrace) {
      log(
        'Error: $error. StackTrace: $stackTrace.',
        error: error,
        stackTrace: stackTrace,
      );
      rethrow;
    }
  }

  Future<void> _login() async {
    try {
      final isInitialized = await _vkIdAuth.isInitialized;
      if (!isInitialized) {
        throw Error.throwWithStackTrace(
          FlutterError(
              'VkIdAuth login method was called before initialization'),
          StackTrace.current,
        );
      }
      data = await _vkIdAuth.login();
      setState(() {});
    } on Object catch (error, stackTrace) {
      log(
        'Error: $error. StackTrace: $stackTrace.',
        error: error,
        stackTrace: stackTrace,
      );
      rethrow;
    }
  }
}

@immutable
class VkIdAuthPage extends StatelessWidget {
  final VkIdAuthData? data;
  final VoidCallback loginCallback;

  const VkIdAuthPage({
    required this.data,
    required this.loginCallback,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('VK ID Auth'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 40),
                child: Text(
                  'Token: ${data?.token}',
                  textAlign: TextAlign.center,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20),
                child: ElevatedButton(
                  onPressed: loginCallback,
                  child: const Text('Login with VK ID'),
                ),
              ),
            ],
          ),
        ),
      );
}
1
likes
0
points
10
downloads

Publisher

verified publisherdev.kurmantaev.ru

Weekly Downloads

A Flutter plugin for auth via VK ID SDK. The plugin allows you to log in using VK ID SDK and receive an access token and user data. If the VK application is installed on the device, then the plugin will try to log in through it, otherwise the browser will be used. Only iOS and ANDROID are supported.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, meta, plugin_platform_interface

More

Packages that depend on vk_id_auth

Packages that implement vk_id_auth