flutter_login_vk 3.0.0 copy "flutter_login_vk: ^3.0.0" to clipboard
flutter_login_vk: ^3.0.0 copied to clipboard

Login via VK.com. Easily add VK login feature in your application. User profile information included.

example/lib/main.dart

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  final plugin = VKLogin(debug: true);

  MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  String? _sdkVersion;
  VKAccessToken? _token;
  VKUserProfile? _profile;
  String? _email;
  bool _sdkInitialized = false;

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

    _getSdkVersion();
    _initSdk();
  }

  @override
  Widget build(BuildContext context) {
    final token = _token;
    final profile = _profile;
    final isLogin = token != null;
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Login via VK example'),
        ),
        body: Padding(
          padding: const EdgeInsets.symmetric(vertical: 18.0, horizontal: 8.0),
          child: Builder(
            builder: (context) => Center(
              child: Column(
                children: <Widget>[
                  if (_sdkVersion != null) Text('SDK v$_sdkVersion'),
                  if (token != null && profile != null)
                    Padding(
                      padding: const EdgeInsets.only(bottom: 10),
                      child: _buildUserInfo(context, profile, token, _email),
                    ),
                  isLogin
                      ? OutlinedButton(
                          onPressed: _onPressedLogOutButton,
                          child: const Text('Log Out'),
                        )
                      : OutlinedButton(
                          onPressed: _onPressedLogInButton,
                          child: const Text('Log In'),
                        ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildUserInfo(
    BuildContext context,
    VKUserProfile profile,
    VKAccessToken accessToken,
    String? email,
  ) {
    final photoUrl = profile.photo200;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text('User: '),
        Text(
          '${profile.firstName} ${profile.lastName}',
          style: const TextStyle(fontWeight: FontWeight.bold),
        ),
        Text(
          'Online: ${profile.online}, Online mobile: ${profile.onlineMobile}',
          style: const TextStyle(fontWeight: FontWeight.bold),
        ),
        if (photoUrl != null) Image.network(photoUrl),
        const Text('AccessToken: '),
        Text(
          accessToken.token,
          softWrap: true,
        ),
        Text('Created: ${accessToken.created}'),
        if (email != null) Text('Email: $email'),
      ],
    );
  }

  Future<void> _onPressedLogInButton() async {
    final res = await widget.plugin.logIn(
      scope: [
        VKScope.email,
      ],
    );

    if (res.isError) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Log In failed: ${res.asError!.error}'),
          ),
        );
      }
    } else {
      final loginResult = res.asValue!.value;
      if (!loginResult.isCanceled) await _updateLoginInfo();
    }
  }

  Future<void> _onPressedLogOutButton() async {
    await widget.plugin.logOut();
    await _updateLoginInfo();
  }

  Future<void> _initSdk() async {
    await widget.plugin.initSdk();
    _sdkInitialized = true;
    await _updateLoginInfo();
  }

  Future<void> _getSdkVersion() async {
    final sdkVersion = await widget.plugin.sdkVersion;
    setState(() {
      _sdkVersion = sdkVersion;
    });
  }

  Future<void> _updateLoginInfo() async {
    if (!_sdkInitialized) return;

    final plugin = widget.plugin;
    final token = await plugin.accessToken;
    final profileRes = token != null ? await plugin.getUserProfile() : null;
    final email = token != null ? await plugin.getUserEmail() : null;

    setState(() {
      _token = token;
      _profile = profileRes?.asValue?.value;
      _email = email;
    });
  }
}
54
likes
150
points
339
downloads

Documentation

API reference

Publisher

verified publisherinnim.ru

Weekly Downloads

Login via VK.com. Easily add VK login feature in your application. User profile information included.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

async, flutter

More

Packages that depend on flutter_login_vk

Packages that implement flutter_login_vk