flutter_zalo_login 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

Flutter Zalo Login

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_zalo_login:

Usage #

Call init function on initState

ZaloLogin().init();

Login function return type https://pub.dev/documentation/flutter_zalo_login/latest/flutter_zalo_login/ZaloLoginResult-class.html | Properties | Type
| ------------- |:-------------:| | userId | String | | errorCode | int | | errorMessage | String | | oauthCode | String |

ZaloLoginResult res = await ZaloLogin().logIn();

Check is authenticated

bool isAuthenticated = await ZaloLogin().isAuthenticated();

Logout

await ZaloLogin().logOut();

Get info from user return type https://pub.dev/documentation/flutter_zalo_login/latest/flutter_zalo_login/ZaloProfileModel-class.html | Properties | Type
| ------------- |:-------------:| | id | String | | name | String | | birthday | String | | gender | String | | picture.data.url | String |

ZaloProfileModel info = await ZaloLogin().getInfo();

Setup Zalo developers #

Go to this page: https://developers.zalo.me/app

Login and create new app

It will have 2 value

ID: <YOUR_ZALO_APP_ID>

Secret Key: <YOUR_ZALO_APP_SECRET_KEY>

Note #

Android will need your <YOUR_ZALO_APP_ID> and Hash Key #

Hash Key will be show on console log when you run ZaloLogin().init(); like below

V/ZaloLogin(28268): Please add this Hash Key to Zalo developers dashboard for Login
V/ZaloLogin(28268): tUDfvw+YYoyciFpRM4WIRYeqtRI=

IOS will need both <YOUR_ZALO_APP_ID> and <YOUR_ZALO_APP_SECRET_KEY> #

After that config your BundleID, Package name, Hash key on zalo develop config

IOS #

Open /ios/Runner/Info.plist and add the following:

    ...
    <key>CFBundleURLTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>CFBundleURLName</key>
			<string>zalo</string>
			<key>CFBundleURLSchemes</key>
			<array>
				<string>zalo-<YOUR_ZALO_APP_ID></string>
			</array>
		</dict>
	</array>
	<key>ZaloAppID</key>
	<string><YOUR_ZALO_APP_ID></string>
	<key>ZaloAppKey</key>
	<string><YOUR_ZALO_APP_SECRET_KEY></string>
    ...

After that, open ios/Runner/AppDelegate.swift and add code below:

    ...
    override func application(
        _ application: UIApplication,
        open url: URL,
        sourceApplication: String?,
        annotation: Any
    ) -> Bool {
        return ZDKApplicationDelegate.sharedInstance().application(application,
            open: url,
            sourceApplication: sourceApplication,
            annotation: annotation
        )
    }

    @available(iOS 9.0, *)
    override func application(
        _ app: UIApplication,
        open url: URL,
        options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

        return ZDKApplicationDelegate
            .sharedInstance()
            .application(app,
                            open: url as URL?,
                            sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String?,
                            annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        )
        return false
    }

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

If your project using Object-c add code to follow file ios/Runner/AppDelegate.m:


...

- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
  return [[ZDKApplicationDelegate sharedInstance] application:application openURL:url options:options];
}

Android #

Note this plugin need config #

minSdkVersion 18

Open to /android/app/src/main/AndroidManifest.xml and add config #

    <application
        android:name=".FlutterApplication" 
        android:label="zaa_example"
        android:icon="@mipmap/ic_launcher">
        
        <!-- ... -->
        <!-- ... -->

        <!-- add new line -->
        <meta-data
            android:name="com.zing.zalo.zalosdk.appID"
            android:value="@string/appID" />
        <activity
                android:name="com.zing.zalo.zalosdk.oauth.BrowserLoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="@string/zalosdk_login_protocol_schema"/>
            </intent-filter>
        </activity>
    </application>

After that create new file /android/app/src/main/res/values/string.xml #

<resources>
    <string name="app_name">zaa2</string>
    <string name="appID"><YOUR_ZALO_APP_ID></string>
    <string name="zalosdk_app_id"><YOUR_ZALO_APP_ID></string>
    <string name="zalosdk_login_protocol_schema">zalo-<YOUR_ZALO_APP_ID></string>
</resources>

Create new file /android/app/src/main/java/com/neun/flutter_zalo_login/flutter_zalo_login_example/FlutterApplication.java #

package com.neun.flutter_zalo_login.flutter_zalo_login_example;

import android.app.Activity;
import android.app.Application;
import androidx.annotation.CallSuper;
import io.flutter.view.FlutterMain;

import com.zing.zalo.zalosdk.oauth.ZaloSDKApplication;

public class FlutterApplication extends Application {
    @Override
    @CallSuper
    public void onCreate() {
        super.onCreate();
        ZaloSDKApplication.wrap(this);
        FlutterMain.startInitialization(this);
    }

    private Activity mCurrentActivity = null;

    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    public void setCurrentActivity(Activity mCurrentActivity) {
        this.mCurrentActivity = mCurrentActivity;
    }
}

After that edit file MainActivity

If Kotlin

    import com.zing.zalo.zalosdk.oauth.ZaloSDK;

    override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        ZaloSDK.Instance.onActivityResult(this, requestCode, resultCode, data)
    }

If Java

    import com.zing.zalo.zalosdk.oauth.ZaloSDK;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      ZaloSDK.Instance.onActivityResult(this, requestCode, resultCode, data);
    }

0.1.2 #

  • [Thien] add auth code func update readme.md

0.1.1 #

  • Fix login by app on IOS not return result.

0.1.0 #

  • update description.

0.0.2 #

  • setup example app.

0.0.1 #

  • init.

example/lib/main.dart

import 'dart:ui';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ZaloLoginResult zaloLoginResult = ZaloLoginResult(
    errorCode: -1,
    errorMessage: "",
    oauthCode: "",
    userId: "",
  );
  ZaloProfileModel zaloInfo = ZaloProfileModel(
    birthday: "",
    gender: "",
    id: "",
    name: "",
    picture: null,
  );
  bool _authenticated = false;

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

  void _loginZalo() async {
    print(await ZaloLogin.channel.invokeMethod('getPlatformVersion'));

    ZaloLoginResult res = await ZaloLogin().logIn();
    setState(() {
      zaloLoginResult = res;
    });
    print(res);
  }

  void _isAuthenticated() async {
    bool isAuthenticated = await ZaloLogin().isAuthenticated();
    print(isAuthenticated);
    setState(() {
      _authenticated = isAuthenticated;
    });
  }

  void _logoutZalo() async {
    await ZaloLogin().logOut();
    setState(() {
      zaloLoginResult = null;
    });
  }

  void _getInfo() async {
    ZaloProfileModel info = await ZaloLogin().getInfo();
    setState(() {
      zaloInfo = info;
    });
    print(info);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Divider(),
            Row(
              children: <Widget>[
                VerticalDivider(),
                SizedBox(
                  child: FlatButton(
                    child: Text(
                      "Login Zalo",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: _loginZalo,
                    color: Theme.of(context).accentColor,
                  ),
                  width: 150,
                ),
                VerticalDivider(),
                if (zaloLoginResult != null)
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          "oauthCode: " + zaloLoginResult.oauthCode.toString(),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                        Text("errorCode: " +
                            zaloLoginResult.errorCode.toString()),
                        Text("userId: " + zaloLoginResult.userId),
                      ],
                    ),
                  ),
              ],
            ),
            Divider(),
            Row(
              children: <Widget>[
                VerticalDivider(),
                SizedBox(
                  width: 150,
                  child: FlatButton(
                    child: Text(
                      "Is Authenticated",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: _isAuthenticated,
                    color: Theme.of(context).accentColor,
                  ),
                ),
                VerticalDivider(),
                Expanded(
                  child: Text(
                    "Is Authenticated: " + _authenticated.toString(),
                    textAlign: TextAlign.start,
                  ),
                ),
              ],
            ),
            Divider(),
            Row(
              children: <Widget>[
                VerticalDivider(),
                SizedBox(
                  width: 150,
                  child: FlatButton(
                    child: Text(
                      "Get info",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: _getInfo,
                    color: Theme.of(context).accentColor,
                  ),
                ),
                VerticalDivider(),
                if (zaloInfo != null)
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        if (zaloInfo != null && zaloInfo.picture != null)
                          Image.network(zaloInfo.picture.data.url),
                        Text(
                          "id: " + zaloInfo?.id,
                        ),
                        Text(
                          "name: " + zaloInfo?.name,
                        ),
                        Text(
                          "birthday: " + zaloInfo?.birthday,
                        ),
                        Text(
                          "gender: " + zaloInfo?.gender,
                        ),
                      ],
                    ),
                  )
              ],
            ),
            Divider(),
            Row(
              children: <Widget>[
                VerticalDivider(),
                SizedBox(
                  width: 150,
                  child: FlatButton(
                    child: Text("Logout"),
                    onPressed: _logoutZalo,
                    color: Theme.of(context).buttonColor,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_zalo_login: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_zalo_login/flutter_zalo_login.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
52
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:flutter_zalo_login/flutter_zalo_login.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:flutter_zalo_login/flutter_zalo_login.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:flutter_zalo_login/flutter_zalo_login.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:flutter_zalo_login/flutter_zalo_login.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [flutter_zalo_login] that is in a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

55 out of 55 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test