Flutter自定义登录按钮插件custom_signin_buttons的使用

Flutter自定义登录按钮插件custom_signin_buttons的使用

介绍

custom_signin_buttons 是一个旨在以更简便和更可定制的方式创建登录按钮的包。

安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  ...
  custom_signin_buttons: ^1.0.0

使用示例

导入 custom_signin_buttons.dart 包:

import 'package:custom_signin_buttons/custom_signin_buttons.dart';
内置按钮示例

以下是内置按钮的使用示例:

// Google 登录按钮
SignInButton(
  Buttons.Google,
)

// 带自定义文本的 Discord 登录按钮
SignInButton(
  button: Button.Discord,
  text: 'Inicia con Discord', // 开始使用 Discord
),
小型按钮示例
// 小型 Facebook 登录按钮
SignInButton(
  Buttons.Facebook,
  mini: true,
)
精简按钮示例
// 精简 Facebook 登录按钮
SignInButton(
  Buttons.Facebook,
  small: true,
)
自定义构建按钮示例
// 自定义电子邮件登录按钮
CustomSignInButton(
  text: 'Sign In With Email', // 使用电子邮件登录
  customIcon: Icons.email,
  buttonColor: Colors.blue,
  iconColor: Colors.grey,
  textColor: Colors.grey,
  mini: false,
)

// 小型自定义图标按钮
CustomSignInButton(
  customIcon: FontAwesomeIcons.envelopeOpen, // 邮件图标
  buttonColor: Colors.blue,
  iconColor: Colors.grey,
  mini: true,
),

注意:onPressed(){} 不是必须的。

内置按钮包含

以下是内置按钮枚举:

enum Button {
  Apple,
  AppleDark,
  Discord,
  Email,
  Facebook,
  FacebookNew,
  GitHub,
  Google,
  GoogleBlue,
  GoogleBlack,
  GooglePlay,
  Instagram,
  InstagramGradient,
  LinkedIn,
  Pinterest,
  PlayStation,
  PlayStationGradient,
  Reddit,
  RedditGradient,
  Steam,
  SteamGradient,
  Spotify,
  SpotifyLight,
  TikTok,
  Twitch,
  TwitchLight,
  Twitter,
  Xbox,
  XboxDark,
  Youtube,
  YoutubeDark,
}

展示图

完整示例 Demo

以下是完整示例代码,展示了如何使用 custom_signin_buttons 插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false, // 关闭调试横幅
        title: 'Material App',
        home: ButtonPage());
  }
}

class ButtonPage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 30), // 水平边距
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // 垂直居中
            children: [
              // Apple Dark 登录按钮
              SignInButton(
                button: Button.AppleDark,
                borderRadius: 15, // 圆角
                height: 40, // 高度
                iconSize: 30, // 图标大小
                mini: false, // 是否小型
                onPressed: () {}, // 点击事件
                splashColor: Colors.pink, // 溅射颜色
                small: false, // 是否精简
                showText: true, // 显示文本
                textSize: 15, // 文本大小
                width: 300, // 宽度
              ),
              // 小型 Apple Dark 登录按钮
              SignInButton(
                button: Button.AppleDark,
                small: true,
                iconSize: 50, // 图标大小
              ),
              SizedBox(height: 10), // 间距
              // 小型 TikTok 登录按钮
              SignInButton(
                button: Button.TikTok,
                small: true,
              ),
              SizedBox(height: 10), // 间距
              // Email 登录按钮
              SignInButton(button: Button.Email),
              SizedBox(height: 10), // 间距
              // 自定义 Facebook 登录按钮
              CustomSignInButton(
                height: 80, // 高度
                width: 80, // 宽度
                iconLeftPadding: 2, // 左侧图标填充
                iconTopPadding: 2, // 上方图标填充
                iconSize: 62, // 图标大小
                customIcon: FontAwesomeIcons.facebookF, // 自定义图标
                iconColor: Colors.white, // 图标颜色
                buttonColor: Colors.blue.shade700, // 按钮颜色
              )
            ],
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用custom_signin_buttons插件来创建自定义登录按钮的代码示例。custom_signin_buttons是一个流行的Flutter插件,它允许你轻松地使用多种社交媒体和电子邮件提供商的样式来创建登录按钮。

首先,你需要在你的pubspec.yaml文件中添加这个依赖:

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

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

接下来,你可以在你的Flutter应用中使用这些自定义按钮。以下是一个简单的示例,展示了如何使用Google、Facebook和Email按钮:

import 'package:flutter/material.dart';
import 'package:custom_signin_buttons/buttons.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Sign-In Buttons Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GoogleSignInButton(
                onPressed: () {
                  // 在这里处理Google登录逻辑
                  print("Google Sign-In Button Pressed");
                },
                darkMode: false, // 根据需要设置是否为暗模式
              ),
              SizedBox(height: 20),
              FacebookSignInButton(
                onPressed: () {
                  // 在这里处理Facebook登录逻辑
                  print("Facebook Sign-In Button Pressed");
                },
                darkMode: false, // 根据需要设置是否为暗模式
              ),
              SizedBox(height: 20),
              EmailSignInButton(
                onPressed: () {
                  // 在这里处理Email登录逻辑
                  print("Email Sign-In Button Pressed");
                },
                buttonStyle: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.blue),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含了三个登录按钮:Google、Facebook和Email。每个按钮的onPressed回调函数中,我们仅打印了一条消息,但在实际应用中,你应该在这里添加相应的登录逻辑。

GoogleSignInButtonFacebookSignInButton都接受一个darkMode参数,用于指定按钮是否应该以暗模式显示。对于EmailSignInButton,你可以通过buttonStyle参数来自定义按钮的样式,例如背景颜色。

这个插件提供了多种登录按钮的样式,你可以根据需要选择合适的按钮来增强你的应用的用户体验。更多详细信息和自定义选项,请参考custom_signin_buttons的官方文档和示例。

回到顶部