Flutter社交登录按钮插件auth_social_buttons的使用

Flutter社交登录按钮插件auth_social_buttons的使用

插件介绍

Flutter widget库,包含用于与流行社交网络(Apple、Google、Facebook、Twitter和Microsoft进行身份验证的按钮。

示例应用截图

使用方法

1 pubspec.yaml 中添加 auth_social_buttons,然后导入 Dart 文件:

import 'package:auth_social_buttons/auth_social_buttons.dart';

使用 onPressed 属性来捕获按钮点击,并在其中调用身份验证逻辑。要禁用按钮,请传递 null 或省略该属性。

FacebookSignInButton(onPressed: () {
  // call authentication logic
});

一些按钮具有暗模式。通过可选参数启用此功能:

GoogleSignInButton(
  onPressed: () {/* ... */}, 
  darkMode: true, // 默认为 false
)

可以调整按钮的边角半径:

TwitterSignInButton(
  onPressed: () {},
  borderRadius: 10.0,
)

可以调整按钮的文本样式:

TwitterSignInButton(
  onPressed: () {},
  textStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.w700, fontFamily: "Roboto"),
)

可以调整按钮的背景色:

GoogleSignInButton(
  onPressed: () {/* ... */}, 
  splashColor: Colors.white, 
  // 将 splashColor 设置为 Colors.transparent 将移除按钮的涟漪效果。
)

按钮可以像普通 Material 按钮一样拉伸。默认情况下,按钮内容向左对齐。您可以选择使用 centered 属性将图标和文本居中对齐。

TwitterSignInButton(
  onPressed: () {},
  centered: true,
)

更多 API 细节请参阅文档:https://pub.dartlang.org/documentation/auth_social_buttons/latest/

贡献指南

贡献非常受欢迎。我建议在花费时间之前先讨论大型更改的问题。

高质量的 pull requests 将赢得您提交的权利。


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

void main() async {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    const padding = 25.0;

    return MaterialApp(
      title: 'Button Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text("auth_social_buttons"),
        ),
        backgroundColor: Color.fromARGB(0FF, 0F, 0o,  oF),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Column(
                children: <Widget>[
                  SizedBox(height: padding),
                  AppleSignInButton(onPressed: () {}),
                  AppleSignInButton(
                      onPressed: () {}, style: AppleButtonStyle.whiteOutline),
                  AppleSignInButton(
                      onPressed: () {}, style: AppleButtonStyle.black),
                  SizedBox(height: padding),
                  GoogleSignInButton(onPressed: () {}),
                  GoogleSignInButton(onPressed: () {}, darkMode: true),
                  SizedBox(height: padding),
                  FacebookSignInButton(onPressed: () {}),
                  SizedBox(height: padding),
                  TwitterSignInButton(onPressed: () {}),
                  SizedBox(height: padding),
                  MicrosoftSignInButton(onPressed: () {}),
                  MicrosoftSignInButton(onPressed: () {}, darkMode: true),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter社交登录按钮插件auth_social_buttons的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter社交登录按钮插件auth_social_buttons的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用auth_social_buttons插件来实现社交登录按钮的示例代码。auth_social_buttons插件允许你轻松地在Flutter应用中集成社交登录按钮,比如Google、Facebook、Apple等。

首先,确保你已经在pubspec.yaml文件中添加了auth_social_buttons依赖:

dependencies:
  flutter:
    sdk: flutter
  auth_social_buttons: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

接下来,你需要配置一些必要的平台特定设置,比如Google登录和Facebook登录所需的配置。这里以Google登录为例进行说明,Facebook和Apple登录的配置类似,但需要在各自的开发者平台进行相应的设置。

Google登录配置

  1. 在Google Cloud Console中创建一个项目,并启用Google Sign-In API。
  2. 下载GoogleService-Info.plist(iOS)或 google-services.json(Android),并将其放置在你的Flutter项目的ios/Runnerandroid/app目录下。
  3. android/app/build.gradle文件中添加Google服务的classpath依赖:
dependencies {
    // ...
    classpath 'com.google.gms:google-services:4.3.10'  // 请使用最新版本
}
  1. android/app目录下创建或编辑google-services.json文件,确保它包含你从Google Cloud Console下载的配置。
  2. android/目录下编辑build.gradle文件,应用Google服务插件:
allprojects {
    repositories {
        google()
        // ...
    }
}

Flutter代码实现

在你的Flutter项目中,你可以这样使用auth_social_buttons

import 'package:flutter/material.dart';
import 'package:auth_social_buttons/auth_social_buttons.dart';
import 'package:auth_social_buttons/sign_in_with_apple_button.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:fluttertoast/fluttertoast.dart';  // 用于显示Toast消息

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SocialLoginScreen(),
    );
  }
}

class SocialLoginScreen extends StatefulWidget {
  @override
  _SocialLoginScreenState createState() => _SocialLoginScreenState();
}

class _SocialLoginScreenState extends State<SocialLoginScreen> {
  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<void> _handleGoogleSignIn() async {
    try {
      final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
      if (googleUser == null) return;

      final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );

      final UserCredential authResult = await _auth.signInWithCredential(credential);
      User? user = authResult.user;

      if (user != null) {
        Fluttertoast.showToast(msg: "Signed in as: ${user.displayName ?? user.email}");
      }
    } catch (e) {
      Fluttertoast.showToast(msg: e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Social Login Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SizedBox(width: 200, height: 50, child: SignInWithGoogle(
              onPressed: _handleGoogleSignIn,
            )),
            // 你可以添加其他社交登录按钮,比如Facebook和Apple
            // SizedBox(width: 200, height: 50, child: SignInWithApple(
            //   onPressed: _handleAppleSignIn,
            // )),
            // SizedBox(width: 200, height: 50, child: SignInWithFacebook(
            //   onPressed: _handleFacebookSignIn,
            // )),
          ],
        ),
      ),
    );
  }
}

注意:

  • 你需要在pubspec.yaml文件中添加firebase_authgoogle_sign_in依赖,以及用于显示Toast消息的fluttertoast依赖。
  • SignInWithAppleSignInWithFacebook按钮的实现类似,但你需要分别在Apple Developer和Facebook Developer平台上进行相应的配置,并在Flutter代码中添加相应的处理逻辑。

这个示例展示了如何使用auth_social_buttons插件来集成Google登录按钮,并处理用户登录后的逻辑。你可以根据需要添加其他社交登录按钮,并处理相应的登录逻辑。

回到顶部