Let’s create custom social login buttons for Google, Facebook, and Apple in Flutter without using any external packages.

Step 1: Set Up Your Flutter Project

If you haven’t already, start by creating a new Flutter project.

flutter create social_login_buttons
cd social_login_buttons

Step 2: Design the UI for the Buttons

We’ll create custom widgets for Google, Facebook, and Apple login buttons.

Step 3: Implement the Social Login Buttons

Create a new file called social_login_buttons.dart in the lib directory.

touch lib/social_login_buttons.dart

In this file, define custom widgets for each button:

import 'package:flutter/material.dart';

class SocialLoginButton extends StatelessWidget {
  final String text;
  final Color color;
  final String logo;
  final VoidCallback onPressed;

  const SocialLoginButton({
    Key? key,
    required this.text,
    required this.color,
    required this.logo,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        primary: color,
        onPrimary: Colors.white,
        minimumSize: Size(double.infinity, 50),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
      onPressed: onPressed,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            logo,
            height: 24,
            width: 24,
          ),
          SizedBox(width: 12),
          Text(
            text,
            style: TextStyle(
              fontSize: 16,
            ),
          ),
        ],
      ),
    );
  }
}

class GoogleLoginButton extends StatelessWidget {
  final VoidCallback onPressed;

  const GoogleLoginButton({Key? key, required this.onPressed}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SocialLoginButton(
      text: "Continue with Google",
      color: Colors.white,
      logo: 'assets/google_logo.png',  // Add your Google logo image in assets
      onPressed: onPressed,
    );
  }
}

class FacebookLoginButton extends StatelessWidget {
  final VoidCallback onPressed;

  const FacebookLoginButton({Key? key, required this.onPressed}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SocialLoginButton(
      text: "Continue with Facebook",
      color: Color(0xFF3b5998),
      logo: 'assets/facebook_logo.png',  // Add your Facebook logo image in assets
      onPressed: onPressed,
    );
  }
}

class AppleLoginButton extends StatelessWidget {
  final VoidCallback onPressed;

  const AppleLoginButton({Key? key, required this.onPressed}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SocialLoginButton(
      text: "Continue with Apple",
      color: Colors.black,
      logo: 'assets/apple_logo.png',  // Add your Apple logo image in assets
      onPressed: onPressed,
    );
  }
}

Step 4: Add Logo Images to Assets

You need to add the logos for Google, Facebook, and Apple to your assets directory. Create an assets directory in the root of your project and add the logo images.

  1. Create the directory: mkdir assets
  2. Add your logo images (e.g., google_logo.png, facebook_logo.png, apple_logo.png) to the assets directory.
  3. Update your pubspec.yaml to include assets: flutter: assets: - assets/google_logo.png - assets/facebook_logo.png - assets/apple_logo.png

Step 5: Use the Buttons in Your App

In your main.dart file, use these custom buttons:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Social Login Buttons"),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              GoogleLoginButton(
                onPressed: () {
                  // Handle Google login
                  print("Google login pressed");
                },
              ),
              SizedBox(height: 16),
              FacebookLoginButton(
                onPressed: () {
                  // Handle Facebook login
                  print("Facebook login pressed");
                },
              ),
              SizedBox(height: 16),
              AppleLoginButton(
                onPressed: () {
                  // Handle Apple login
                  print("Apple login pressed");
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Step 6: Run Your App

Run your app to see the custom social login buttons in action.

flutter run

Customizations

  • Text and Logo: You can change the text and logo images according to your needs.
  • Colors: Modify the color parameter in each button widget to change the button colors.
  • Button Style: Adjust the ElevatedButton.styleFrom properties to customize the button’s appearance further.

This guide helps you create custom social login buttons without using external packages, allowing you to have full control over the design.

Output

Here is the image of the custom social login buttons for Google, Facebook, and Apple as described in the code.

Related Articles


Deepika

Hey, I'm Deepika, Experienced in Mobile app Development (Flutter, Android and iOS) and professional blogger. Technically sound Post graduated M.Tech in Computer Science and Engineering. I Love to gain every type of knowledge that's why i have done many courses in different fields like engineering and technology. Skilled in Flutter,( Dart ), Java, HTML, CSS, PHP, Python, SQL, C, C++,Firebase,MySQL,SQLite,JavaScript, Networking, Ethical Hacking.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *